【问题标题】:Getting text into div with python beautifulsoup使用 python beautifulsoup 将文本放入 div
【发布时间】:2018-08-01 18:34:02
【问题描述】:

我正在学习 python 和 lib beatifulsoup 来做一些爬虫,我想从 html 代码中提取文本,但文本不在标签中。看代码

我的 bs4 代码是这样的:

for p in soup.find_all("div", class_="description__container-text"):
    v1 = p
    for x in v1:
        print(v1)

输出如下:

<div class="description__container-text" itemprop="description"> 
    <h2 class="description__product-title">
        TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT” HD
    </h2> 
    <p class="description__text"></p> 
    *********I WANT TO EXTRACT THIS TEXT HEAR!!!!!!*************************************************** 
    <p class="description__text"></p> 
    <p class="description__text"> 
        <center>
            <iframe frameborder="0" height="6500" src="https://myurl.html" width="100%"></iframe>
        </center>

那么,我怎样才能得到文本:

*********I WANT TO EXTRACT THIS TEXT HEAR!!!!!!***************************************************

【问题讨论】:

标签: python beautifulsoup


【解决方案1】:

一种方法是从该 div 元素中获取所有文本,然后使用 str.replace() 减去您不想要的其他文本

这是一个删除 h2 文本并修剪最终结果以去除空白的示例

from bs4 import BeautifulSoup


doc = """
<div class="description__container-text" itemprop="description"> <h2                          class="description__product-title">TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT” HD</h2>
 <p class="description__text"></p> *********I WANT TO EXTRACT THIS TEXT                       HEAR!!!!!!***************************************************
    <p class="description__text"></p>
 <p class="description__text"> <center><iframe frameborder="0" height="6500" src="https://    myurl.html" width="100%"></iframe></center>
</p>
</div>
"""
soup = BeautifulSoup(doc, 'html.parser')
print("all text: %s" % soup.get_text())
allText = soup.get_text()
h2Text = soup.find('h2').get_text()
# separate h2 text
print("h2 text: %s" %h2Text)
# remove h2 text from all text
modifiedAllText = allText.replace(h2Text, '')
print("Modified: %s" % modifiedAllText.strip())

【讨论】:

    【解决方案2】:

    这取决于你想如何指定你想要的项目。我将假设您想要指定您想要的文本,它位于p 元素和class 'description_text' 之后。但我描述的方法更普遍。

    首先找到一个包含您要查找的元素的元素。在这种情况下,我选择了div。然后浏览该元素的后代,直到看到紧接在您想要的那个之前的那个。

    出于说明目的(以及我自己的懒惰),我没有以循环的形式编写此代码。使用items = div.descendantsitems 中获取迭代器。然后使用next依次获取div的每个后代,在item中。

    如果item.name 为空,则item 为字符串,否则item.name 为标签命名。当您看到上面提到的p 标签时,您就知道下一项将是您真正想要的文本项。

    >>> import bs4
    >>> soup = bs4.BeautifulSoup(open('bueno.htm').read(), 'lxml')
    >>> div = soup.find('div')
    >>> items = div.descendants
    >>> item = next(items)
    >>> item.name
    >>> item
    ' '
    >>> item = next(items)
    >>> item.name
    'h2'
    >>> item
    <h2 class="description__product-title">TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT” HD</h2>
    >>> item = next(items)
    >>> item.name
    >>> item
    'TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT” HD'
    >>> item = next(items)
    >>> item.name
    >>> item
    ' '
    >>> item = next(items)
    >>> item.name
    'p'
    >>> item
    <p class="description__text"></p>
    >>> item = next(items)
    >>> item.name
    >>> item
    ' *********I WANT TO EXTRACT THIS TEXT HEAR!!!!!!*************************************************** \n    '
    

    【讨论】:

    • 谢谢比尔,这个页面工作得很好,我只是不确定其他页面的 HTML 结构是否相同,为许多“相等”页面做一个爬虫。无论如何,谢谢你
    • 不客气,蒂亚戈。这是解析 HTML 的大问题。很多时候,这些技术只适用于某些情况。如果您愿意,请在其中一个页面上发布另一个问题,然后在此处对我发表评论。我很乐意看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多