python - .get_text() 在 BeautifulSoup 中不起作用
问题描述
为什么汤不识别.get_text()
删除 HTML 元素的功能?
我不断收到此错误消息:
File "./bondora.py", line 45, in <module>
onlineumsaetze.append(td.get_text())
File "/usr/local/lib/python3.8/dist-packages/bs4/element.py", line 2173, in __getattr__
raise AttributeError(
AttributeError: ResultSet object has no attribute 'get_text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
我的代码:
#!/usr/bin/python3
from selenium import webdriver
from seleniumrequests import Firefox
from pyvirtualdisplay import Display
from bs4 import BeautifulSoup
onlineumsaetze = []
url = browser.page_source
soup = BeautifulSoup(url, 'lxml')
tab = soup.find('table', {"class":"table js-filter-table"})
for row in tab.find_all('tr'):
td = row.find_all("td")
onlineumsaetze.append(td.get_text())
print(onlineumsaetze)
解决方案
.find_all()
返回 BS 对象的列表,您不能使用 . 从列表中提取文本.get_text()
。您必须在该列表中再次迭代,并为每个元素使用get_text()
.
for row in tab.find_all('tr'):
td = row.find_all("td")
for element in td:
onlineumsaetze.append(element.get_text())
推荐阅读
- react-native - 付款完成后获取信用卡号
- c# - 如何在运行时更改 url 徽标
- regex - 正则表达式无法对两个匹配项使用相同的字符
- html - CSS 元素出现在当前行下方。怎么修?
- xcode - 如何消除有关 MobileCoreServices 和 AssetsLibrary 的 Xcode 11.4 警告?
- php - 双击单个链接
- python - 如何将 python 模块 timeago 与烧瓶一起使用?
- javascript - 我如何根据两个选择选项打开弹出窗口
- jquery - 使用jsp更新MySQL表中的记录失败
- php - 使用laravel将普通密码IN数据库转换为加密密码?