如何在网络抓取风速 google 时解决属性错误

How to get resolve of attribute error when web scraping google for wind speed

我正在网络抓取 google 他们的风速,直到几周前这段代码工作得很好!现在我已经 运行 它并且不断收到“AttributeError:'NoneType' object has no attribute 'text'”错误。我可以改变什么让它像以前一样工作?我很困惑为什么它停止工作了!

import requests
import re
from bs4 import BeautifulSoup as bs

#Function that returns windpseed in a dictionary
def current_windspeed(url):
    #Defining user agent and language to scrape google
    USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
    #Setting language as UK english
    LANGUAGE = "en-UK,en;q=0.5"
    session=requests.Session()
    session.headers['User-Agent']= USER_AGENT
    session.headers['Accept-Language']= LANGUAGE
    session.headers['Content-Language']=LANGUAGE
    #downloads html code for google weather london
    html=session.get(url)
    #creates a new soup
    soup=bs(html.text, "html.parser")
    #dictionary where windspeed will be stored
    current_weather={}
    print(current_weather)
    current_weather['wind']=soup.find("span",attrs={"id": "wob_ws"}).text
    return current_weather

它标记的问题是这行代码

current_weather['wind']=soup.find("span",attrs={"id": "wob_ws"}).text

我到底要怎样才能让它重新工作,为什么它停止工作了?

解析假设您始终从服务器取回良好数据。我全天候 24/7 加载天气数据,它可以持续好几天甚至几周,但服务器偶尔不会很好地播放,并在极少数情况下给你一个 404 或一个空文件或其他任何东西。

代码位:

soup.find("span",attrs={"id": "wob_ws"})

没有找到任何东西,所以它 returns None。

您的代码可以编写为接受错误输入并在记录问题时优雅地继续。您通常可以假设它稍后会自行修复并使用 try/except 并记录错误并在下一次获取它。如果服务器更改了网页,那么您可能需要重新编写代码。