Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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获取div中div的内容?_Python_Beautifulsoup - Fatal编程技术网

Python 使用BeautifulSoup获取div中div的内容?

Python 使用BeautifulSoup获取div中div的内容?,python,beautifulsoup,Python,Beautifulsoup,我想获取一个div的内容,该div的类为“gt read”,并且在该div中有另一个具有不同类的div。下面是脚本代码片段: 脚本: data = """ <div class='gt-read'> <!-- no need --> <!-- some no need --> <b>Bold text</b> - some text here <br/>

我想获取一个div的内容,该div的类为“gt read”,并且在该div中有另一个具有不同类的div。下面是脚本代码片段:

脚本:

data = """
    <div class='gt-read'>
        <!-- no need -->
        <!-- some no need -->

        <b>Bold text</b> - some text here <br/>
        lorem ipsum here <br/>
        <strong> Author Name</strong>

        <div class='some-class'>
            <script>
                #...
                Js script here
                #...
            </script>
        </div>
    </div>
    """
soup = BeautifulSoup(data, 'lxml')
get_class = soup.find("div", {"class" : "detail_text"})
print 'notices', notices.get_text()
print 'notices', notices
data=”“”
粗体文本-此处的一些文本
lorem ipsum在此
作者姓名 #... Js脚本在这里 #... """ soup=BeautifulSoup(数据'lxml') get_class=soup.find(“div”,{“class”:“detail_text”}) 打印“通知”,通知。获取文本() 打印“通知”,通知
我想要这样的结果:

<b>Bold text</b> - some text here <br/>
lorem ipsum here <br/>
<strong> Author Name</strong>
粗体文本-此处有一些文本
lorem ipsum在此
作者姓名

请提供帮助。

以下内容应显示您需要的内容:

from bs4 import BeautifulSoup, Comment  

data = """
    <div class='gt-read'>
        <!-- no need -->
        <!-- some no need -->

        <b>Bold text</b> - some text here <br/>
        lorem ipsum here <br/>
        <strong> Author Name</strong>

        <div class='some-class'>
            <script>
                #...
                Js script here
                #...
            </script>
        </div>
    </div>
    """
soup = BeautifulSoup(data, 'lxml')
get_class = soup.find("div", {"class" : "gt-read"})
comments = get_class.find_all(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]

get_class.find("div").extract()
text = get_class.encode_contents().strip()

print text
来自bs4导入美化组的
,注释
data=”“”
粗体文本-此处的一些文本
lorem ipsum在此
作者姓名 #... Js脚本在这里 #... """ soup=BeautifulSoup(数据'lxml') get_class=soup.find(“div”,“class”:“gt read”}) comments=get\u class.find\u all(text=lambda text:isinstance(text,Comment)) [comment.extract()用于注释中的注释] get_class.find(“div”).extract() text=get_class.encode_contents().strip() 打印文本
为您提供以下输出:

<b>Bold text</b> - some text here <br/>
        lorem ipsum here <br/>
<strong> Author Name</strong>   
粗体文本-此处有一些文本
lorem ipsum在此
作者姓名

这将获取
gt read
类,提取所有注释和div标记,并返回剩余的标记。

这是我案例中的工作,谢谢。这很有帮助!