首页 > 解决方案 > .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)

标签: pythonbeautifulsoup

解决方案


.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())

推荐阅读