我的示例代码:
>>> 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()?
有人可以帮我理解吗?
答案 0 :(得分:0)
您可以使用以下代码获取所需的内容:
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