python - 从美丽的汤中过滤掉标签
问题描述
我正在查看网站上的表格,我基本上需要扫描表格中的每十个项目,然后将这些值导出到项目的 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'
解决方案
tds = soup.findAll('td')
是一个列表。您可以尝试遍历列表并获得所需的输出。
前任:
tds = soup.findAll('td')[2::10]
for td in tds:
print(td.text)
推荐阅读
- django - Django:'str'对象在create_user中没有属性'objects'
- javascript - 即使声明了变量,也出现未定义的错误
- javascript - 最小化窗口/浏览器选项卡时 setInterval 函数无法正常工作
- vector - 使用 mmapLayerComboBox 中的选定图层
- swift - 工作表和警报不断刷新我的 WKWebView SwiftUI 页面
- python - 每个进程的速度随着更多进程而变慢
- python - 如何在 keras 中使用 elmoformanylangs?
- tomcat - 如果我调用 connector.start(),Tomcat 9 会抱怨启动两次?
- javascript - CryptoJS 解密值与原始值不同
- prolog - 尝试获取组合时参数没有充分实例化