【问题标题】:How can I extract this text using BeautifulSoup?如何使用 BeautifulSoup 提取此文本?
【发布时间】:2017-11-03 09:17:00
【问题描述】:

我正在尝试使用 beautifulsoup 从 beeradvocate 中获取评论。审核代码如下所示:

[<span class="BAscore_norm">4.49</span>,
 <span class="rAvg_norm">/5</span>,
 u'\xa0\xa0rDev ',
 <span style="color:#006600;">+2%</span>,
 <br/>,
 <span class="muted">look: 4.25 | smell: 4.5 | taste: 4.5 | feel: 4.5 |  
 overall: 4.5</span>,
 <br/>,
 <br/>,
 u'Pours a slightly hazy golden orange with two fingers white head. ',
 <br/>,
 u'\nSmells of citrus, orange, pineapple, sweet malty presence.',
 <br/>,
 u'\nTastes starts with the juicy orange, pineapple. Finishes with a 
 somewhat sweet caramel toffee like malt presence.',
 <br/>,
 u'\nVery smooth medium body. Alchohol was very well hidden until it started 
 to warm a bit.',
 <br/>,
 u'\nOverall a really tasty brew!',
 <br/>,
 <br/>,
 <i aria-hidden="true" class="fa fa-file-text-o"></i>,
 u'\xa0',
 <span class="muted">354 characters</span>,
 <br/>,
 <br/>,
 <div><span class="muted"><a class="username" 
href="/community/members/jbowengeorgia.1171914/">JBowenGeorgia</a>, <a 
href="/beer/profile/26/1558/?ba=JBowenGeorgia#review">Oct 03, 2017</a>
</span></div>]

我不知道如何提取评论的文本。 Python BeautifulSoup extract text between element 有一个类似的问题,但大多数答案都涉及 .contents 和位置参数,由于评论中段落之间的换行符,这在此处不起作用。

【问题讨论】:

  • 如果有初始的 html 代码,而不仅仅是来自 python 的打印,那就太好了。

标签: python beautifulsoup


【解决方案1】:

试试这个单行:

text = ''.join(x for x in soup if type(x) == bs4.NavigableString and not x.startswith(u'\xa0'))

这里soup对应标签&lt;div id="rating_fullview_content_2"&gt;。我不知道你是否有这个变量,但soup.content 匹配你在原始问题中给出的代码块。

【讨论】:

    【解决方案2】:

    假设你将页面的初始html代码放入html变量中:

    # -*- coding: utf-8 -*-
    
    import bs4
    
    if __name__=="__main__":
        with open('page.html') as page:
            html = page.read()
            soup = bs4.BeautifulSoup(html, 'lxml')
    
            reviews = soup.br.find_next_siblings(text=True)
            reviews = map(lambda x: x.strip(), reviews)  # remove whitespace
            reviews = filter(lambda x: bool(x), reviews)  # remove empty strings
    
            for review in reviews:
                print "REVIEW:", review
    

    这会给你类似的东西:

    REVIEW: Pours a slightly hazy golden orange with two fingers white head.
    REVIEW: Smells of citrus, orange, pineapple, sweet malty presence.
    REVIEW: Tastes starts with the juicy orange, pineapple. Finishes with a
     somewhat sweet caramel toffee like malt presence.
    REVIEW: Very smooth medium body. Alchohol was very well hidden until it started
    to warm a bit.
    REVIEW: Overall a really tasty brew!
    REVIEW: \xa0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2021-10-14
      • 2021-10-05
      相关资源
      最近更新 更多