我试图从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:18:06
错误的原因:
没有
<div>和类ten-day-page-title。这就是你犯错误的原因。而且,没有带有类<table>的twc-table。
你可以试着这样做。
<details>标签中,它的id以detailIndex开头。我已经使用re来选择所有这样的<detail>标记<detail>标记中提取所需的任何信息。
在这里,我已经打印了预测摘要。
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']发布于 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
复制相似问题