我正试图从维基百科头版的“维基百科的其他领域”部分删除文本。但是,我遇到了错误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)
在您的代码中
otherAreasContainer
是类型ResultSet
,并且ResultSet
没有.find_all()
方法要从
"Other areas of Wikipedia"
下选择所有<li>
,可以使用CSS选择器h2:contains("Other areas of Wikipedia") + div li
例如:
印刷品:
有关CSS Selectors的详细信息
运行我得到的代码
这应该是你问题的一部分-让我们更容易发现你的问题
find_all
返回一个ResultSet
,它本质上是找到的元素列表。您需要枚举每个元素才能继续find_all
的结果是一个列表,而该列表没有find
或find_all
属性,您必须迭代otherAreasContainer
,然后对其调用find_all
方法,如下所示:相关问题 更多 >
编程相关推荐