首页 > 解决方案 > 从美丽的汤中过滤掉标签

问题描述

我正在查看网站上的表格,我基本上需要扫描表格中的每十个项目,然后将这些值导出到项目的 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'

标签: pythonweb-scrapingbeautifulsoup

解决方案


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

前任:

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

推荐阅读