Python 检索具有相同跨度类别和嵌套跨度的bbc天气数据
我试图从英国广播公司天气数据中提取数据,以便在家庭自动化仪表板中使用 我可以很好地提取HTML代码,我可以提取一组临时值,但它只提取第一组临时值 坐 13摄氏度55华氏度 5摄氏度41华氏度 风速 31公里/小时19英里/小时 东北偏东 这是我的代码,它不工作Python 检索具有相同跨度类别和嵌套跨度的bbc天气数据,python,beautifulsoup,Python,Beautifulsoup,我试图从英国广播公司天气数据中提取数据,以便在家庭自动化仪表板中使用 我可以很好地提取HTML代码,我可以提取一组临时值,但它只提取第一组临时值 坐 13摄氏度55华氏度 5摄氏度41华氏度 风速 31公里/小时19英里/小时 东北偏东 这是我的代码,它不工作 import urllib2 import pprint from bs4 import BeautifulSoup htmlFile=urllib2.urlopen('http://www.bbc.co.uk/weather/2646
import urllib2
import pprint
from bs4 import BeautifulSoup
htmlFile=urllib2.urlopen('http://www.bbc.co.uk/weather/2646504?day=1')
htmlData = htmlFile.read()
soup = BeautifulSoup(htmlData)
table=soup.find("div","daily-window")
temperatures=[str(tem.contents[0]) for tem in table.find_all("span",class_="units-value temperature-value temperature-value-unit-c")]
mintemp=[str(min.contents[0]) for min in table.find_("span",class_="min-temp min-temp-value")]
maxtemp=[str(min.contents[0]) for min in table.find_all("span",class_="max-temp max-temp-value")]
windspeeds=[str(speed.contents[0]) for speed in table.find_all("span",class_="units-value windspeed-value windspeed-value-unit-mph")]
pprint.pprint(zip(temperatures,temp2,windspeeds))
您的最小和最大温度提取错误。您只需找到包含c和f格式的孔最小温度范围。获取内容的第一项内容将为您提供空字符串 min-temp标签identification class=min-temp.min-temp-value与c-type min-temp class=temperature-value-unit-c不同,因此我建议您使用css选择器 例如,找到您的所有最低温度范围可能是
table.select('span.min-temp.min-temp-value span.temperature-value-unit-c')
这意味着选择所有class=temperature-value-unit-c跨度,它们是class=min-temp-min-temp-value跨度的子级
其他信息列表也一样,比如max_temp wind