【问题标题】:Filtering BeautifulSoup find_all results过滤 BeautifulSoup find_all 结果
【发布时间】:2012-12-15 08:52:10
【问题描述】:

我正在尝试从亚马逊收集一些书籍评论。到目前为止,这是我所拥有的:

import requests
from bs4 import BeautifulSoup

def data(site):
    url = site
    r = requests.get(url)
    soup = BeautifulSoup(r.text) 
    y = soup.find_all("div", style = "margin-left:0.5em;")
    words = []
    for item in y:
        item = str(item.text).split()
        words.append(item)
    reviews = [" ".join(x) for x in words]
    return reviews

f = data('http://www.amazon.com/Dance-Dragons-Song-Fire-Book/product-reviews/0553801473/ref=cm_cr_pr_top_link_11?ie=UTF8&pageNumber=11&showViewpoints=0&sortBy=bySubmissionDateDescending')

除了评论之外,我还获得了一些无关信息,例如作者、标题和认为评论有帮助的人数。有没有办法使用 BeautifulSoup 来排除除评论文本之外的所有内容?评论的文本没有类或样式属性,而其他文本有(我认为......),但我还没有找到过滤我的 soup.find_all 结果的方法。非常感谢任何帮助。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    所有评论都包含在table 中,因此您可以先找到表格,然后从每个评论中提取评论文本。

    改变这一行应该可以做到:

    ...
    y = soup\
        .find('table', {'id' : 'productReviews'})\ # here you find the table
        .find_all("div", style = "margin-left:0.5em;")
    ...
    

    【讨论】:

      猜你喜欢
      • 2019-08-22
      • 2017-07-17
      • 2017-03-13
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 2017-06-12
      • 1970-01-01
      相关资源
      最近更新 更多