我试图从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和整个库都很陌生。我希望有人能帮我解决这个问题。
发布于 2021-11-10 05:00:34
几件事:
正如注释中所指出的,
。
因此,重写可能类似于:
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
titlehttps://stackoverflow.com/questions/69905610
复制相似问题