美丽的汤找到了,找到了所有的

2024-06-16 15:07:36 发布

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

我的示例代码:

>>> from bs4 import BeautifulSoup
>>> from requests import get
>>> url = 'https://nationaldaycalendar.com/what-is-national-today/'
>>> response = get(url)
>>> html_soup = BeautifulSoup(response.text, 'html.parser')
>>> national_container = html_soup.find_all('div', class_ = 'eventon_events_list')
>>> container = national_container.find_all(itemprop='description').get('content')
This line ^ Resulted in an error

引发的错误为:

Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    container = national_container.find_all(itemprop='description').get('content')
  File "C:\Users\hyuiu\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bs4\element.py", line 2160, in __getattr__
    raise AttributeError(
AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

有人能帮我理解吗


Tags: infromimporturlgetresponsecontainerhtml
1条回答
网友
1楼 · 发布于 2024-06-16 15:07:36

您可以使用此代码获取所需的内容:

from bs4 import BeautifulSoup
from requests import get
url = 'https://nationaldaycalendar.com/what-is-national-today/'
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
national_container = html_soup.find_all('div', class_ = 'eventon_events_list')
container = national_container[0].find('meta', {'itemprop':'description'}).get('content')
print(container)

将输出打印为:

NATIONAL CHOCOLATE MILKSHAKE DAY

相关问题 更多 >