【问题标题】:Parsing HTML with BeautifulSoup用 BeautifulSoup 解析 HTML
【发布时间】:2012-12-13 07:47:51
【问题描述】:

(图小,这里是另一个链接:http://i.imgur.com/OJC0A.png

我正在尝试提取底部的评论文本。我试过这个:

y = soup.find_all("div", style = "margin-left:0.5em;")
review = y[0].text

问题在于未展开的div 标记中有不需要的文本,从评论内容中删除这些文本变得乏味。对于我的生活,我无法弄清楚这一点。有人可以帮我吗?

编辑:HTML 是:

div style="margin-left:0.5em;">
    <div style="margin-bottom:0.5em;"> 9 of 35 people found the following review helpful </div>
    <div style="margin-bottom:0.5em;">
    <div style="margin-bottom:0.5em;">
    <div class="tiny" style="margin-bottom:0.5em;">
        <b>
    </div>
    That is true. I tried it myself this morning. There's a little note on the Audible site that says "a few titles will require two credits" or something like that. A Dance with Dragons is one of those few. 

文字上方的div标签如下:

<div class="tiny" style="margin-bottom:0.5em;">
    <b>
        <span class="h3color tiny">This review is from: </span>
        <a href="https://rads.stackoverflow.com/amzn/click/com/B005C7QVUE" rel="nofollow noreferrer">A Dance with Dragons: A Song of Ice and Fire: Book 5 (Audible Audio Edition)</a>
    </b>
</div>
That is true. I tried it myself this morning. There's a little note on the Audible site that says "a few titles will require two credits" or something like that. A Dance with Dragons is one of those few. 

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    http://www.crummy.com/software/BeautifulSoup/bs4/doc/#strings-and-stripped-strings 建议 .strings 方法是您想要的 - 它返回对象中每个字符串的迭代器。所以如果你把那个迭代器变成一个列表并取最后一项,你应该得到你想要的。例如:

    $ python
    >>> import bs4
    >>> text = '<div style="mine"><div>unwanted</div>wanted</div>'
    >>> soup = bs4.BeautifulSoup(text)
    >>> soup.find_all("div", style="mine")[0].text
    u'unwantedwanted'
    >>> list(soup.find_all("div", style="mine")[0].strings)[-1]
    u'wanted'
    

    【讨论】:

      【解决方案2】:

      获取div.tiny尾部的文字:

      review = soup.find("div", "tiny").findNextSibling(text=True)
      

      完整示例:

      #!/usr/bin/env python
      from bs4 import BeautifulSoup
      
      html = """<div style="margin-left:0.5em;">
      <div style="margin-bottom:0.5em;">
         9 of 35 people found the following review helpful </div>
      <div style="margin-bottom:0.5em;">
      <div style="margin-bottom:0.5em;">
      <div class="tiny" style="margin-bottom:0.5em;">
      <b>
          <span class="h3color tiny">This review is from: </span>
          <a href="http://rads.stackoverflow.com/amzn/click/B005C7QVUE">
           A Dance with Dragons: A Song of Ice and Fire: Book 5 (Audible Audio Edition)</a>
      </b>
      </div>
      That is true. I tried it myself this morning. There's a little note on the Audible site that says "a few titles will require two credits" or something like that. A Dance with Dragons is one of those few."""
      
      soup = BeautifulSoup(html)
      review = soup.find("div", "tiny").findNextSibling(text=True)
      print(review)
      

      输出

      那是真实的。今天早上我自己试过了。 Audible 网站上有一个小注释,上面写着“一些标题需要两个学分”或类似的东西。 《魔龙狂舞》就是其中之一。

      这是产生相同输出的等效 lxml 代码:

      import lxml.html
      
      doc = lxml.html.fromstring(html)
      print doc.find(".//div[@class='tiny']").tail
      

      【讨论】:

      • 抱歉造成混淆,文本不在该类 tiny 的 div 标签下。它位于主 div 标签下,样式为 margin-left:0.5em;
      • @user1709173:有用吗?如果没有,请发布提供足够上下文的实际 html(作为文本,而不是图片),即在文本周围包含元素。 “tail”紧跟在“tiny”元素之后,因此下一个兄弟元素应该可以工作。
      • 对不起。我在原始帖子的编辑中发布了 HTML。展开嵌套的 div 标签最终会显示文本,但它会有点长,所以我的编辑中没有包含。
      • @user1709173:我已经用你的 html 尝试了我的代码,它可以工作。你得到了什么结果?
      • 我得到'\n'。我在原始帖子中发布了带有小类的 div 标签的扩展。很抱歉造成混乱...
      猜你喜欢
      • 1970-01-01
      • 2013-03-10
      • 2020-02-06
      • 2014-03-06
      • 2011-07-21
      • 2011-09-24
      • 2018-07-10
      • 2021-10-31
      • 2014-09-03
      相关资源
      最近更新 更多