【问题标题】:AttributeError: 'NoneType' object has no attribute 'find' BeautifulSoup soup.find errorAttributeError: 'NoneType' 对象没有属性 'find' BeautifulSoup soup.find 错误
【发布时间】:2021-12-22 14:23:39
【问题描述】:

我正在尝试从 weather.com 中抓取天气信息,但由于某种原因它无法正常工作,这是错误:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    all=soup.find("div",{"class":"locations-title ten-day-page-title"}).find("h1").text
AttributeError: 'NoneType' object has no attribute 'find'

我不知道这里出了什么问题。 代码如下:

import requests
from bs4 import BeautifulSoup
page = requests.get("https://weather.com/en-IE/weather/tenday/l/bf217d537cc1c8074ec195ce07fb74de3c1593caa6033b7c3be4645ccc5b01de")
soup = BeautifulSoup(page.content,"html.parser")

all=soup.find("div",{"class":"locations-title ten-day-page-title"}).find("h1").text
table=soup.find_all("table",{"class":"twc-table"})
l=[]
for items in table:
 for i in range(len(items.find_all("tr"))-1):
  d = {}  
  d["day"]=items.find_all("span",{"class":"date-time"})[i].text
  d["date"]=items.find_all("span",{"class":"day-detail"})[i].text
  d["desc"]=items.find_all("td",{"class":"description"})[i].text 
  d["temp"]=items.find_all("td",{"class":"temp"})[i].text 
  d["precip"]=items.find_all("td",{"class":"precip"})[i].text
  d["wind"]=items.find_all("td",{"class":"wind"})[i].text  
  d["humidity"]=items.find_all("td",{"class":"humidity"})[i].text 
  l.append(d)

我真的不明白这里发生了什么,因为我是 python 和整个库的新手。我希望有人可以帮助我解决这个问题。

【问题讨论】:

  • 第一个soup.find返回None,这意味着该页面上不存在div和class标签,您应该确保检查您是否拥有正确的标签(可以很简单作为拼写错误)

标签: python html web-scraping beautifulsoup python-requests


【解决方案1】:

有几点:

  1. 如 cmets 中所述,您的第一个 soup.find 返回 None 因此错误。查看相关标题,类名有动态部分。我会改为使用属性 = 值 css 选择器,以运算符开头,通过子字符串定位类属性。我还将添加 h1 类型选择器,从而确保直接定位 h1 而无需链接调用。

  2. 避免使用已经是关键字或python内置的变量名,即all()。

因此,重写可能如下所示:

import requests
from bs4 import BeautifulSoup

page = requests.get("https://weather.com/en-IE/weather/tenday/l/bf217d537cc1c8074ec195ce07fb74de3c1593caa6033b7c3be4645ccc5b01de")
soup = BeautifulSoup(page.content,"html.parser")
title = soup.select_one('h1[class^=LocationPageTitle]').text
title

【讨论】:

    【解决方案2】:

    错误原因:

    没有&lt;div&gt; 类ten-day-page-title。这就是您收到该错误的原因。也没有 &lt;table&gt; 类 twc-table。

    你可以试试这个方法。

    • 未来 10 天的天气预报显示在 &lt;details&gt; 标签中,其 id 以 detailIndex 开头。我使用re 来选择所有这些&lt;detail&gt; 标签
    • 现在您可以从上面选定的&lt;detail&gt; 标签中提取您需要的任何信息

    这里我打印了预测摘要。

    import re
    import requests
    from bs4 import BeautifulSoup
    page = requests.get("https://weather.com/en-IE/weather/tenday/l/bf217d537cc1c8074ec195ce07fb74de3c1593caa6033b7c3be4645ccc5b01de")
    soup = BeautifulSoup(page.content,"lxml")
    
    d = soup.find_all('details', {'id': re.compile(r'^detailIndex')})
    
    for i in d:
        p = i.find('summary')
        print(list(p.stripped_strings))
    
    ['Tonight', '--', '/', '9°', 'Cloudy', 'Cloudy', 'Rain', '15%', 'Wind', 'NNW', '9 km/h', 'Arrow Up']
    ['Wed 10', '16°', '/', '3°', 'Scattered Showers', 'AM Showers', 'Rain', '42%', 'Wind', 'NW', '18 km/h', 'Arrow Down']
    ['Thu 11', '12°', '/', '8°', 'Partly Cloudy', 'Partly Cloudy', 'Rain', '3%', 'Wind', 'ENE', '11 km/h', 'Arrow Down']
    ['Fri 12', '17°', '/', '6°', 'Rain', 'Rain', 'Rain', '89%', 'Wind', 'SSE', '32 km/h', 'Arrow Down']
    ['Sat 13', '14°', '/', '4°', 'Partly Cloudy', 'Partly Cloudy', 'Rain', '23%', 'Wind', 'N', '15 km/h', 'Arrow Down']
    ['Sun 14', '11°', '/', '2°', 'Mostly Sunny', 'Mostly Sunny', 'Rain', '8%', 'Wind', 'W', '21 km/h', 'Arrow Down']
    ['Mon 15', '9°', '/', '2°', 'Scattered Showers', 'Showers', 'Rain', '40%', 'Wind', 'WNW', '17 km/h', 'Arrow Down']
    ['Tue 16', '9°', '/', '2°', 'Mostly Sunny', 'Mostly Sunny', 'Rain', '14%', 'Wind', 'WNW', '22 km/h', 'Arrow Down']
    ['Wed 17', '10°', '/', '5°', 'Mostly Sunny', 'Mostly Sunny', 'Rain', '7%', 'Wind', 'W', '21 km/h', 'Arrow Down']
    ['Thu 18', '14°', '/', '7°', 'Mostly Sunny', 'Mostly Sunny', 'Rain', '7%', 'Wind', 'SW', '21 km/h', 'Arrow Down']
    ['Fri 19', '14°', '/', '4°', 'Scattered Showers', 'PM Showers', 'Rain', '32%', 'Wind', 'SW', '17 km/h', 'Arrow Down']
    ['Sat 20', '10°', '/', '2°', 'Partly Cloudy', 'Partly Cloudy', 'Rain', '24%', 'Wind', 'WNW', '19 km/h', 'Arrow Down']
    ['Sun 21', '9°', '/', '4°', 'Partly Cloudy', 'Partly Cloudy', 'Rain', '19%', 'Wind', 'NNW', '17 km/h', 'Arrow Down']
    ['Mon 22', '8°', '/', '3°', 'Scattered Showers', 'AM Showers', 'Rain', '32%', 'Wind', 'N', '19 km/h', 'Arrow Down']
    ['Tue 23', '8°', '/', '2°', 'Scattered Showers', 'Few Showers', 'Rain', '34%', 'Wind', 'NW', '19 km/h', 'Arrow Down']
    

    【讨论】:

    • 如果我要制作一个 GUI,我将如何获得第一行中的第一位信息,例如:“Wed 10”
    • 只从页面中抓取特定项目并将其显示在 GUI 中。我已经为您提供了一种从该页面获取完整预测信息的方法。
    猜你喜欢
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    相关资源
    最近更新 更多