【问题标题】:Filtering out tags from beautiful soup从美丽的汤中过滤掉标签
【发布时间】:2018-05-24 01:27:42
【问题描述】:

我正在查看网站上的表格,我基本上需要扫描表格中的每十个项目,然后将这些值导出到项目的 csv 中。这就是我现在正在做的:

prices = []
    for td in soup.findAll('tr'):
    tds = soup.findAll('td')
    prices.append(tds[2::10])

但这会打印出所有的 td 标签。我试着打电话:

prices = []
    for td in soup.findAll('tr'):
    tds = soup.findAll('td')
    print(tds[2::10].text)

但是当我这样做时,我得到了这个错误:

AttributeError: 'list' object has no attribute 'text'

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    tds = soup.findAll('td') 是一个列表。您可以尝试遍历列表并获得所需的输出。

    例如:

    tds = soup.findAll('td')[2::10]
    for td in tds:
        print(td.text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多