【问题标题】:Beautiful Soup Find, Find_all美丽的汤查找,Find_all
【发布时间】:2020-12-31 02:03:17
【问题描述】:

我的示例代码:

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

谁能帮我理解这个?

【问题讨论】:

    标签: python web beautifulsoup screen-scraping


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 2020-07-17
      • 1970-01-01
      • 2014-07-29
      • 2021-06-04
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2018-04-12
      相关资源
      最近更新 更多