python获取天气分析_Python爬取南京市往年天气预报,使用pyecharts进行分析

时间: 2024-03-29 09:35:57 浏览: 16
获取南京市往年天气预报可以通过爬取相关网站的数据来实现。我推荐使用Python中的requests和BeautifulSoup库来实现数据爬取和解析。以下是示例代码: ```python import requests from bs4 import BeautifulSoup year = 2020 # 爬取的年份 url = f'http://www.tianqihoubao.com/lishi/nanjing/month/{year}{str(1).zfill(2)}.html' # 目标网站URL # 发送请求获取HTML文本 response = requests.get(url) html = response.content.decode('gbk') # 解析HTML文本获取天气数据 soup = BeautifulSoup(html, 'html.parser') table = soup.find('table', attrs={'class': 'b'}) rows = table.find_all('tr')[1:] # 第一行为表头,去掉 for row in rows: data = row.find_all('td') date = data[0].text.replace('\n', '') # 日期 high = data[1].text.replace('\n', '') # 最高气温 low = data[2].text.replace('\n', '') # 最低气温 weather = data[3].text.replace('\n', '') # 天气 wind_direction = data[4].text.replace('\n', '') # 风向 wind_power = data[5].text.replace('\n', '') # 风力 print(date, high, low, weather, wind_direction, wind_power) ``` 获取到天气数据后,可以使用pyecharts进行数据可视化分析。以下是示例代码: ```python from pyecharts.charts import Line from pyecharts import options as opts # 处理数据 x_data = [f'{year}-{str(i).zfill(2)}' for i in range(1, 13)] # x轴数据 y_data_high = [] # 最高气温数据 y_data_low = [] # 最低气温数据 for month in range(1, 13): url = f'http://www.tianqihoubao.com/lishi/nanjing/month/{year}{str(month).zfill(2)}.html' response = requests.get(url) html = response.content.decode('gbk') soup = BeautifulSoup(html, 'html.parser') table = soup.find('table', attrs={'class': 'b'}) rows = table.find_all('tr')[1:] high_temps = [int(row.find_all('td')[1].text) for row in rows] low_temps = [int(row.find_all('td')[2].text) for row in rows] y_data_high.append(sum(high_temps) / len(high_temps)) y_data_low.append(sum(low_temps) / len(low_temps)) # 绘制折线图 line = Line() line.add_xaxis(x_data) line.add_yaxis('最高气温', y_data_high) line.add_yaxis('最低气温', y_data_low) line.set_global_opts(title_opts=opts.TitleOpts(title='南京市{}年气温变化'.format(year))) line.render('nanjing_temp.html') ``` 以上代码会生成一个HTML文件,可以在浏览器中打开查看绘制好的折线图。

相关推荐

最新推荐

recommend-type

基于Python获取城市近7天天气预报

主要介绍了基于Python获取城市近7天天气预报,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

python使用Word2Vec进行情感分析解析

python实现情感分析(Word2Vec) ** 前几天跟着老师做了几个项目,老师写的时候劈里啪啦一顿敲,写了个啥咱也布吉岛,线下自己就瞎琢磨,终于实现了一个最简单的项目。输入文本,然后分析情感,判断出是好感还是反感...
recommend-type

python实现网络爬虫 爬取北上广深的天气数据报告 python.docx

该资源是python实现网络爬虫 爬取北上广深的天气数据的报告 注:可用于期末大作业实验报告
recommend-type

Python模块包中__init__.py文件功能分析

主要介绍了Python模块包中__init__.py文件功能,简单分析了__init__.py在调入模块和包的过程中的作用,需要的朋友可以参考下
recommend-type

Python中if __name__ == '__main__'作用解析

主要介绍了Python中if __name__ == '__main__'作用解析,这断代码在Python中非常常见,它有作用?本文就解析了它的作用,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。