-
import requests from bs4 import BeautifulSoup from pyecharts.charts import Bar ALL_DATA = [] def parse_page(url,jiex): header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; ' 'Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', # 'Referer': 'http://www.weather.com.cn/textFC/hb.shtml' } # 模拟浏览器 response = requests.get(url=url, headers=header) # 发送get命令 text = response.content.decode('utf-8') # text用requests库获取 soup = BeautifulSoup(text,jiex) # 以lxml方式解析 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') chengs = list(tds[-8].stripped_strings)[0] # 倒数第8列为城市信息 maxtemp = list(tds[-5].stripped_strings)[0] # 倒数第5列为最高气温 mintemp = list(tds[-2].stripped_strings)[0] # 倒数第2列为最低气温 ALL_DATA.append({"城市":chengs,"最高气温":int(maxtemp),"最低气温":int(mintemp)}) 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' } for url in urls: parse_page(url,'lxml') url_gat = 'http://www.weather.com.cn/textFC/gat.shtml' parse_page(url_gat,'html5lib')#容错能力强的html5lib对港澳台进行解析 # 分析数据 ALL_DATA.sort(key=lambda data:data["最低气温"]) min_data = ALL_DATA[:10] ALL_DATA.sort(key=lambda data:data["最高气温"]) max_data = ALL_DATA[-10:-1] # 数据可视化 cities_min = list(map(lambda x:x["城市"], min_data)) cities_max = list(map(lambda x:x["城市"], max_data)) min_temp = list(map(lambda x:x["最低气温"], min_data)) max_temp = list(map(lambda x: x["最高气温"], max_data)) bar1 = Bar() bar1.add_xaxis(cities_min) bar1.add_yaxis("气温/℃", min_temp) bar1.set_global_opts(title_opts={"text": "中国城市气温排行榜", "subtext": "最低气温"}) bar1.render("最低气温.html") bar2 = Bar() bar2.add_xaxis(cities_max) bar2.add_yaxis("气温/℃", max_temp) bar2.set_global_opts(title_opts={"text": "中国城市气温排行榜", "subtext": "最高气温"}) bar2.render("最高气温.html") if __name__ == '__main__': main()
然后出现了以下问题,求大神指点
我应该怎么改