爬取中国天气网全部地区的天气,最高气温与最低气温状况(python是3.7版本的)

"""
time:2019.9.25
people:旧纸
"""

import requests
from bs4 import BeautifulSoup
ALL_DATA = [] ##列表装获取的地点,天气,最高气温,最低气温
def parse_page(url): ##定义一个函数,分析页面列表
headers = { ##获取页面头部信息
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
'Referer':'http://www.weather.com.cn/textFC/hn.shtml'
}
response = requests.get(url,headers = headers) ##获取网页
text = response.content.decode("utf-8") ##编译成网页赋值给text
soup = BeautifulSoup(text,'html5lib')

conMidtab = soup.find('div',class_='conMidtab') ##获取网页中‘div’,class = ‘conMidtab’的元素conMidtab
tables = conMidtab.find_all('table')                             ##获取conMidtab里面的table元素
for table in tables:
trs = table.find_all('tr')[2:]
for index,tr in enumerate(trs):
tds = tr.find_all('td')
city_td = tds[0] ###获取城市
if index == 0:
city_td = tds[1]
city = list(city_td.stripped_strings)[0] ###获取城市
temp_td = tds[-2]
##获取最低温度
temp = list(temp_td.stripped_strings)[0] #获取最低温度
weather_td = tds[4] ##获取天气
if index == 0:
weather_td = tds[5]
weather = list(weather_td.stripped_strings)[0] ##获取天气
max_temp_td = tds[3] ##获取最高气温
if index == 0:
max_temp_td = tds[4]
max_temp = list(max_temp_td.stripped_strings)[0]
#ALL_DATA.append({'城市:':city,'天气:':weather,'最高气温':max_temp,'最低气温':temp})
# print(ALL_DATA)
print({'城市:':city,'天气:':weather,'最高气温':max_temp,'最低气温':temp})
def main():
region = ['hb','db','hd','hz','hn','xb','xn'] #,'gat'
for x in region:
url = 'http://www.weather.com.cn/textFC/{}.shtml'.format(x)
parse_page(url)
# url = 'http://www.weather.com.cn/textFC/gat.shtml'
# parse_page(url)
#分析数据
##提取前面十个最冷的气温(排序)

if __name__ == '__main__':
main()

效果图

相关文章
相关标签/搜索