为什么ResultSet对象没有“find”属性?

2024-09-29 22:32:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从维基百科头版的“维基百科的其他领域”部分删除文本。但是,我遇到了错误ResultSet object has no attribute 'find'。我的代码出了什么问题?如何使其工作

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)

Tags: textimporturlresponseallfindrequests领域
3条回答

在您的代码中otherAreasContainer是类型ResultSet,并且ResultSet没有.find_all()方法

要从"Other areas of Wikipedia"下选择所有<li>,可以使用CSS选择器h2:contains("Other areas of 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.

有关CSS Selectors的详细信息

运行我得到的代码

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

这应该是你问题的一部分-让我们更容易发现你的问题

find_all返回一个ResultSet,它本质上是找到的元素列表。您需要枚举每个元素才能继续

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)

find_all的结果是一个列表,而该列表没有findfind_all属性,您必须迭代otherAreasContainer,然后对其调用find_all方法,如下所示:

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)

相关问题 更多 >

    热门问题