Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将beautifulsoup中的findall()限制为html的一部分_Python_Html_Beautifulsoup_Html Parsing - Fatal编程技术网

Python 将beautifulsoup中的findall()限制为html的一部分

Python 将beautifulsoup中的findall()限制为html的一部分,python,html,beautifulsoup,html-parsing,Python,Html,Beautifulsoup,Html Parsing,这是我的情况,我用这段代码很好地删除了这个html,但是我找不到如何将第一部分和第二部分分开。我只想刮掉第一部分,把第二部分分开。使用beautifulsoup4 不介意myData(link)是urlopen和html读取函数 html文件 <div id="first_content" class="header"> <div class="list"> <div class="row"> <a n

这是我的情况,我用这段代码很好地删除了这个html,但是我找不到如何将第一部分和第二部分分开。我只想刮掉第一部分,把第二部分分开。使用beautifulsoup4

  • 不介意myData(link)是urlopen和html读取函数
html文件

<div id="first_content" class="header">
    <div class="list">
        <div class="row">
            <a name="03049302"></a>
            <div class="col-xs-12 drop-panel-content">
                <p>
                    first section first text. </p>
            </div>
            <div class="drop-panel drop-panel-one-row-height">
                <p class="text-center">Edit</p>
                <p class="text-center">Share</p>
            </div>
        </div>
        <div class="row">
            <a name="03049303"></a>
            <div class="col-xs-12 drop-panel-content">
                <p>

                    first section second text. </p>
            </div>
            <div class="drop-panel drop-panel-one-row-height">
                <p class="text-center">Edit</p>
                <p class="text-center">Share</p>
                <section id="second_content">
                    <a name="aname" class="btn-collapse collapsed" data-toggle="collapse" data-target="#aname">
                        <h3>A Name</h3>
                    </a>
                    <div class="collapse flush-width flush-down" id="aname">
                        <div class="list">
                            <div class="row">
                                <a name="03049304"></a>
                                <div class="col-xs-12 drop-panel-content">
                                    <p>

                                        second section first text. </p>
                                </div>
                                <div class="drop-panel drop-panel-one-row-height">
                                    <p class="text-center">Edit</p>
                                    <p class="text-center">Share</p>
                              </div>
**在不相同的输出中分开

电流输出

first section first text.
first section second text.
second section first text.
first section first text.
first section second text.
想要的输出

first section first text.
first section second text.
second section first text.
first section first text.
first section second text.
和想要的输出,除了在另一个函数中可能

second section first text.

一个选项是使用
section
标记区分各个部分。第二部分位于
部分
标记内,但第一部分不在标记内

all_data = soup.find_all("div", {"class": "col-xs-12 drop-panel-content"})
for data in all_data:
    if data.find_parent("section") is None:
        print data.get_text(strip=True)
或者,如果严格来说有两个第一节文本,只需将第一节文本列表切成薄片:

all_data = soup.find_all("div", {"class": "col-xs-12 drop-panel-content"})[:2]
for data in all_data:
    print data.get_text(strip=True)

那么,您的预期输出是什么<代码>“第一节第一个文本。”?这意味着什么?除了第二个,只需修正并将我的预期输出添加到问题中,谢谢,我用这个
strip=True
区分部分就解决了很多问题,因为有两个以上的文本,这只是一个例子,非常感谢!!!!!!!