python 爬虫可视化,天气
2024-10-20 20:33:37
网站地址='https://lishi.tianqi.com/chengdu/201704'
import matplotlib.pyplot as plt
import requests
from lxml import etree
from numpy import *
import re
if __name__ == '__main__':
k = []
l = []
for i in range(21,22):
for j in range(1,13):
if j<10:
url = "https://lishi.tianqi.com/chengdu/20%s0%s.html"%(i,j)
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36 Edg/100.0.1185.29'}
res = requests.get(url,headers=headers)
res.encoding = res.apparent_encoding
tree = etree.HTML(res.text)
es = tree.xpath('//ul[@class="thrui"]/li/div/text()')#温度
a=es[1:-1:5]#最高气温,用切片
b=es[2:-1:5]#最低气温,用切片
# 1.将score_list列表转换为以“,”为分隔符的字符串
list_string = ",".join(a)
# 2.使用re模块下的方法——findall对name_score_list_string进行正则匹配,提取出所有的数值型字符串
score_list = re.findall(r"\d+", list_string)
# 3.将得到的score_list中的元素转换为int型
list_int = list(map(int, score_list))
f = mean(list_int)#求平均数
f1=round(f,0) ist=",".join(b)
score_lis =re.findall(r"\d+",ist)
list_in =list(map(int,score_lis))
g=mean(list_in)
g1=round(g,0)
k.append(int(f1))
l.append(int(g1))
x1 = range(1, 10)
x2 = range(1, 10)
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文标签
plt.rcParams['axes.unicode_minus'] = False
plt.plot(x1, k, label='最高温度', linewidth=3, color='r', marker='o',
markerfacecolor='blue', markersize=12)
plt.plot(x1, l, label='最低温度')
plt.xlabel('月份')
plt.ylabel('气温')
# 添加图形标题
plt.title('每月平均最高气温波动趋势')
plt.legend()
# 显示图形
plt.show()
最新文章
- 【转】VS2010中 C++创建DLL图解
- [SLAM] 02 Some algorithms of 3D reconstruction
- 《Redis设计与实现》读书笔记
- DB2物化表
- 百度UEditor开发案例(JSP)
- 一个测ip和端口是否联通的工具类
- 【转】Android(4.2) Sensors 学习——G-sensor,Gyroscope驱动移植
- Boost Lockfree
- (转)提高代码质量---one
- SVN的安装以及和eclipse的结合使用
- 【Java编程】Java中的大整数计算
- linux pwd命令 显示当前所在路径
- Daily record-November
- web.xml中的ContextLoaderListener和DispatcherServlet区别
- 【Zookeeper系列】ZooKeeper伸缩性(转)
- kafka中生产者和消费者API
- js 中的break continue return
- JDBC的简单操作
- cxf之Caused by: java.lang.RuntimeException: Soap 1.1 endpoint already registered on address /rest
- centos solr4.5 tomcat 简单安装[已测试ok]