1:html5lib,lxml , html.parser的比较使用
2:pyecharts图形制作库,制作可视化图形数据
3:map函数的使用
4:lambda的使用
上述小点要学习。。。。。。
# -*- coding: utf-8 -*-
# @Time : 2019/1/26 0026 17:08
# @Author : huangtao
# @Site :
# @File : 3.中国天气网.py
# @Software: PyCharm
# @Blog :https://blog.csdn.net/Programmer_huangtao
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from pyecharts import Bar
ALL_DATA = []
def parse_page(url):
HEADERS = {
'User-Agent': UserAgent().chrome}
res = requests.get(url,headers=HEADERS)
# print(res.content.decode('utf-8'))
text = res.content
soup = BeautifulSoup(text,'html5lib')
conMidtab = soup.find('div',attrs={'class':'conMidtab'})
tables = conMidtab.find_all('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]
min_temp = list(temp_td.stripped_strings)[0]
ALL_DATA.append({"city":city,"min_temp":int(min_temp)})
# print({"city":city,"min_temp":int(min_temp)})
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/hd.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/xn.shtml',
'http://www.weather.com.cn/textFC/gat.shtml'
]
for url in urls:
parse_page(url)
ALL_DATA.sort(key=lambda data:data['min_temp'])
data = ALL_DATA[0:10]
cities = list(map(lambda x:x['city'],data))
temps = list(map(lambda x:x['min_temp'],data))
chart = Bar('中国天气最低气温')
chart.add('',cities,temps)
chart.render('wendu.html')
print(ALL_DATA)
if __name__ == '__main__':
main()
![](https://i-blog.csdnimg.cn/blog_migrate/0058d9f4811e5bd9e75f0d887602fc93.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/16910bb7fb02da43eabaf0097d87a7c8.jpeg)