网站地址='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()

最新文章

  1. 【转】VS2010中 C++创建DLL图解
  2. [SLAM] 02 Some algorithms of 3D reconstruction
  3. 《Redis设计与实现》读书笔记
  4. DB2物化表
  5. 百度UEditor开发案例(JSP)
  6. 一个测ip和端口是否联通的工具类
  7. 【转】Android(4.2) Sensors 学习——G-sensor,Gyroscope驱动移植
  8. Boost Lockfree
  9. (转)提高代码质量---one
  10. SVN的安装以及和eclipse的结合使用
  11. 【Java编程】Java中的大整数计算
  12. linux pwd命令 显示当前所在路径
  13. Daily record-November
  14. web.xml中的ContextLoaderListener和DispatcherServlet区别
  15. 【Zookeeper系列】ZooKeeper伸缩性(转)
  16. kafka中生产者和消费者API
  17. js 中的break continue return
  18. JDBC的简单操作
  19. cxf之Caused by: java.lang.RuntimeException: Soap 1.1 endpoint already registered on address /rest
  20. centos solr4.5 tomcat 简单安装[已测试ok]

热门文章

  1. NSIS安装界面无虚线框移动
  2. CPS攻击案例(一)——基于脉冲宽度调制PWM的无人机攻击
  3. 利用POI遍历出层级结构的excel表格
  4. 浅谈MySQL、Hadoop、BigTable、Clickhouse数据读写机制
  5. C# String.IsNullOrEmpty()方法的使用
  6. 三十四、kubernetes证书介绍
  7. 基于FPGA的SATA3.0主机控制器IP
  8. Vue ref 和 v-for 结合(ref 源码解析)
  9. JK触发器与模12计数器
  10. Datatable 数据源