python爬虫:对全国城市温度进行爬取并可视化

编程入门 行业动态 更新时间:2024-10-11 19:23:42

python<a href=https://www.elefans.com/category/jswz/34/1770264.html style=爬虫:对全国城市温度进行爬取并可视化"/>

python爬虫:对全国城市温度进行爬取并可视化

python爬虫:对全国城市温度进行爬取并可视化

最近在学习爬虫和大数据,学习之余写了简单的demo,注释里面也说的很清楚,小白也都能看懂。如果有什么好的想法或者纠错之处希望能够评论区指出~

import requests
from bs4 import BeautifulSoup
from pyecharts.charts import Bar
from pyecharts import optionsALL_DATA = []def parse_page(url):headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'}response = requests.get(url, headers=headers)# 这个加上了一个header就可以实现访问网页的身份以正常用户进行,如果不进行处理将会使用python身份# 很多的网站会有反爬虫机制。短时间之内多次访问会造成禁止这个ip继续爬取数据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:]  # 这个就是从而开始,把0,1忽略掉# for tr in trs:# tds = tr.find_all('td')# city_td = tds[1]  # 这里开头面对于一个省的第一个城市就会有问题,因为多了一个rowspan,# 但是如果对于直辖市还是取第一个td标签其实也是一样的,反而更加简单# 所以这里做优化for index, tr in enumerate(trs):# enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,# 同时列出数据和数据下标,一般用在 for 循环当中tds = tr.find_all('td')city_td = tds[0]if index == 0:city_td = tds[1]city = list(city_td.stripped_strings)[0]  # 因为使用stripped_strings返回的是一个迭代器,# 所以这里要加上一个list()进行选取显示temp_td = tds[-2]  # 这个-2的意思是取tds标签里面的倒数第二个的意思!!!!min_temp = list(temp_td.stripped_strings)[0]#print({'city': city, 'min_temp': min_temp})ALL_DATA.append({'city': city, 'min_temp': int(min_temp)})def main():url_hb = '.shtml#'url_db = '.shtml'url_hn = '.shtml'url_hz = '.shtml'url_xn = '.shtml'url_gat = '.shtml'# 港澳台网页对lxml解析器就没法较好实现,使用html5liburl_xb = '.shtml'urls = ['.shtml#','.shtml','.shtml','.shtml','.shtml','.shtml','.shtml']# parse_page(url_hb)for url in urls:parse_page(url)# 分析数据# 根据最低气温进行排序ALL_DATA.sort(key=lambda data: data['min_temp'])data = ALL_DATA[0:10]data_max = ALL_DATA[-10:]cities = []temps = []cities = list(map(lambda x: x['city'], data))#print(cities)temps = list(map(lambda x: x['min_temp'], data))cities_max = list(map(lambda x: x['city'], data_max))temp_max = list(map(lambda x: x['min_temp'], data_max))print(cities_max)print(temp_max)#print(temps)# pyecharts库bar = (Bar()# chart.add_xaxis("city",).add_xaxis(cities).add_yaxis('temperature', temps)# .add_xaxis(cities_max)# .add_yaxis("temperature",temp_max,gap='100%')# 加上了gap就是两个y之间的间距是为多少格.set_global_opts(title_opts=options.TitleOpts(title="中国天气最低气温排行榜")))bar.render('temp.html')# 上面还有一种写法如下:chart=Bar()chart.set_global_opts(title_opts=options.TitleOpts(title="中国天气最低温度排行榜"))chart.add_xaxis(cities_max)chart.add_yaxis('temp',temp_max)chart.render('test.html')abc=(Bar().add_xaxis(cities_max).add_yaxis('temp',temp_max).set_global_opts(title_opts=options.TitleOpts(title="a")))abc.render('test2.html')# 还有一种就是通过matplotlib库实现绘图# from matplotlib import plot as plt# 这个库就是m基于matlab的绘图库,基本上实现差不多 ,但是对于中文字符没法较好显示# 后面要添加一下数据处理的过程,比如说什么最小二乘法做曲线的线性化
if __name__ == '__main__':main()

更多推荐

python爬虫:对全国城市温度进行爬取并可视化

本文发布于:2024-02-25 19:28:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爬虫   温度   城市   全国   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!