import requests
from bs4 import BeautifulSoup
from pyecharts.charts import Bar
ALL_DATA = []
def parser_page(url):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}
response = requests.get(url,headers=headers)
text = response.content.decode('utf-8')
soup = BeautifulSoup(text,'html5lib')
conMidtab = soup.find('div',class_='conMidtab')
tables = conMidtab.find_all('table')
for table in tables:
trs = table.find_all('tr')[2:]
for tr in trs:
tds = tr.find_all('td')
city_td = tds[0]
city = list(city_td.stripped_strings)[0]
temperature = list(tds[-2].stripped_strings)[0]
ALL_DATA.append({'city':city,'temperature':int(temperature)})
# print({'city':city,'temperature':temperature})
def main():
urls = [
'http://www.weather.com.cn/textFC/hb.shtml#',
'http://www.weather.com.cn/textFC/db.shtml#',
'http://www.weather.com.cn/textFC/hb.shtml#',
'http://www.weather.com.cn/textFC/hz.shtml#',
'http://www.weather.com.cn/textFC/hn.shtml#',
'http://www.weather.com.cn/textFC/xb.shtml#',
'http://www.weather.com.cn/textFC/hb.shtml#',
'http://www.weather.com.cn/textFC/xn.shtml#',
'http://www.weather.com.cn/textFC/gat.shtml#',
]
for url in urls:
parser_page(url)
ALL_DATA.sort(key=lambda data:data['temperature'])
data = ALL_DATA[:10]
citys = list(map(lambda x:x['city'],data))
temps = list(map(lambda x:x['temperature'],data))
# print(citys,temps)
bar = Bar()
bar.add_xaxis(citys)
bar.add_yaxis("最低气温",temps)
bar.render('temperature.html')
if __name__ == '__main__':
main()
2020年3月17日跟着视频加了一条可视化的操作。