Python 为什么ResultSet对象没有“find”属性?
我正试图把维基百科头版上维基百科其他部分的内容删掉。但是,我遇到错误ResultSet对象没有属性“find”。我的代码出了什么问题?如何使其工作Python 为什么ResultSet对象没有“find”属性?,python,beautifulsoup,Python,Beautifulsoup,我正试图把维基百科头版上维基百科其他部分的内容删掉。但是,我遇到错误ResultSet对象没有属性“find”。我的代码出了什么问题?如何使其工作 import requests from bs4 import BeautifulSoup url = 'https://en.wikipedia.org/' response = requests.get(url) soup = BeautifulSoup(response.text, 'lxml' ) otherAreasContainer =
import requests
from bs4 import BeautifulSoup
url = 'https://en.wikipedia.org/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml' )
otherAreasContainer = soup.find_all('div', class_='mp-bordered')
otherAreasContainerTexts = otherAreasContainer.find_all('li')
for otherAreasContainerText in otherAreasContainerTexts:
print(otherAreasContainerText.text)
运行我得到的代码
Traceback (most recent call last):
File "h.py", line 7, in <module>
otherAreasContainerTexts = otherAreasContainer.find_all('li')
File "/home/td/anaconda3/lib/python3.7/site-packages/bs4/element.py", line 1620, in __getattr__
"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
在代码中,OtherAreaContainer的类型为ResultSet,而ResultSet没有.find_all方法
要从Wikipedia的其他区域下选择all,可以使用CSS选择器h2:Containes Wikipedia的其他区域+div li
例如:
import requests
from bs4 import BeautifulSoup
url = 'https://en.wikipedia.org/'
soup = BeautifulSoup(requests.get(url).content, 'lxml')
for li in soup.select('h2:contains("Other areas of Wikipedia") + div li'):
print(li.text)
印刷品:
Community portal – Bulletin board, projects, resources and activities covering a wide range of Wikipedia areas.
Help desk – Ask questions about using Wikipedia.
Local embassy – For Wikipedia-related communication in languages other than English.
Reference desk – Serving as virtual librarians, Wikipedia volunteers tackle your questions on a wide range of subjects.
Site news – Announcements, updates, articles and press releases on Wikipedia and the Wikimedia Foundation.
Village pump – For discussions about Wikipedia itself, including areas for technical issues and policies.
更多信息。查找所有内容的结果是一个列表,并且列表没有“查找”或“查找所有内容”属性,您必须迭代otherAreasContainer,然后对其调用“查找所有内容”方法,如下所示:
import requests
from bs4 import BeautifulSoup
url = 'https://en.wikipedia.org/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
otherAreasContainer = soup.find_all('div', class_='mp-bordered')
for other in otherAreasContainer:
otherAreasContainerTexts = other.find_all('li')
for otherAreasContainerText in otherAreasContainerTexts:
print(otherAreasContainerText.text)
完整的堆栈跟踪将有所帮助。我们看不到是哪个行号导致了错误。请发布完整的回溯,以便我们可以看到导致问题的extact错误文本和行。可能find_all返回一个列表或迭代器,而您正在尝试对列表对象执行find_all?谢谢,但我从主页的其他部分获得了元素,如艺术传记地理历史数学科学社会技术等所有门户网站“列表未找到属性”是什么意思?谢谢。列表是其他语言中类似python的数组中的数据类型之一,列表本身支持append not find或find_all等方法,这些方法仅适用于bs4对象。
import requests
from bs4 import BeautifulSoup
url = 'https://en.wikipedia.org/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
otherAreasContainer = soup.find_all('div', class_='mp-bordered')
for other in otherAreasContainer:
otherAreasContainerTexts = other.find_all('li')
for otherAreasContainerText in otherAreasContainerTexts:
print(otherAreasContainerText.text)