当前位置:网站首页>How can I get a city's year-round weather data for free?Precipitation, temperature, humidity, solar radiation, etc.
How can I get a city's year-round weather data for free?Precipitation, temperature, humidity, solar radiation, etc.
2022-08-03 12:11:00 【Geographic Remote Sensing Ecological Network】
Weather data has always been a high value data,它被广泛用于各个领域的研究当中.气象数据包括有气温、气压、相对湿度、降水、蒸发、风向风速、日照等多种指标,但是包含了这些全部指标的气象数据却较难获取,即使获取到了也不能随意分享.
If you want to crawl on a large scale,You need to write your own crawler,I wrote a crawl before深圳市data crawler.There is basically no problem with crawling weather data in Shenzhen.
import requests
import demjson
import re
import calendar
import csv
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36\
(KHML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
}
def get_url(date):
url = 'https://www.timeanddate.com/scripts/cityajax.php?n=china/shenzhen&mode=historic'
url += '&hd=' + date
url += '&month=' + str(int(date[4:6]))
url += '&year=' + date[:4] + '&json=1'
return url
# input: type(str) eg:'20170601'
def crawl_single_day(date):
response = requests.get(get_url(date), headers=headers)
response_list = demjson.decode(response.text)
for weather in response_list:
w_time = re.compile(r'^\d+:\d+').search(weather['c'][0]['h']).group(0)
w_temperature = re.compile(
r'^-?\d+').search(weather['c'][2]['h']).group(0)
w_weather = re.compile(
r'^(.*?)\.').search(weather['c'][3]['h']).group(1)
if weather['c'][4]['h'] == 'No wind':
w_wind_speed = '0'
else:
w_wind_speed = re.compile(
r'^\d+').search(weather['c'][4]['h']).group(0)
w_wind_direction = re.compile(
r'title=\"(.*?)\"').search(weather['c'][5]['h']).group(1)
w_humidity = weather['c'][6]['h']
w_barometer = re.compile(r'^\d+').search(weather['c'][7]['h']).group(0)
w_visibility = weather['c'][8]['h']
if w_visibility != 'N/A':
w_visibility=re.compile(r'^\d+').search(w_visibility).group(0)
yield [date, w_time, w_temperature, w_weather, w_wind_speed, w_wind_direction,
w_humidity, w_barometer, w_visibility]
# input: type(int) eg: year=2017, month=6
def crawl_single_month(year, month):
_, num_day = calendar.monthrange(year, month)
month_str = str(year)
if month < 10:
month_str += '0' + str(month)
else:
month_str += str(month)
day_list = list(range(1, num_day + 1))
for day in day_list:
if day < 10:
for weather in crawl_single_day(month_str + '0' + str(day)):
yield weather
else:
for weather in crawl_single_day(month_str + str(day)):
yield weather
if __name__ == "__main__":
with open('weather0.csv', 'w', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
writer.writerow('date time temperature weather wind_speed wind_direction humidity barometer visibility'.split())
for month in range(7, 13):
writer.writerows(crawl_single_month(2017, month))
with open('weather1.csv', 'w', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
writer.writerow('date time temperature weather wind_speed wind_direction humidity barometer visibility'.split())
writer.writerows(crawl_single_day('20210401'))对 20210401The Shenzhen weather data is obtained by crawling csv 文件如下图所示:
当然啦,If the demand is high,可以通过地理遥感生态网平台获取气象数据.
地理遥感生态网平台发布的气象数据包括有气温、气压、相对湿度、降水、蒸发、风向风速、Sunshine solar radiation and many other indicators.
1级目录
文件名
PRS
SURF_CLI_CHN_MUL_DAY-PRS-10004-YYYYMM.TXT(本站气压)
TEM
SURF_CLI_CHN_MUL_DAY-TEM-12001-YYYYMM.TXT(气温)
RHU
SURF_CLI_CHN_MUL_DAY-RHU-13003-YYYYMM.TXT(相对湿度)
PRE
SURF_CLI_CHN_MUL_DAY-PRE-13011-YYYYMM.TXT(降水)
EVP
SURF_CLI_CHN_MUL_DAY-EVP-13240-YYYYMM.TXT(蒸发)
WIN
SURF_CLI_CHN_MUL_DAY-WIN-11002-YYYYMM.TXT(风向风速)
SSD
SURF_CLI_CHN_MUL_DAY-SSD-14032-YYYYMM.TXT(日照)
GST
SURF_CLI_CHN_MUL_DAY-GST-12030-0cm-YYYYMM.TXT(0cm地温)
赶紧三连关注下, 数据获取途径如下:

边栏推荐
- Vs 快捷键---探索不一样的编程
- 深度学习中数据到底要不要归一化?实测数据来说明!
- 【必读要点】Pod控制器Deployment更新、回退详解
- 日常开发写代码原则
- OFDM 十六讲 4 -What is a Cyclic Prefix in OFDM
- bash for loop
- Take you understand the principle of CDN technology
- R语言ggplot2可视化:使用ggpubr包的ggline函数可视化折线图、设置add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图
- LeetCode-48. 旋转图像
- 苹果发布 AI 生成模型 GAUDI,文字生成 3D 场景
猜你喜欢

Matlab学习11-图像处理之图像变换

"Digital Economy Panorama White Paper" Financial Digital User Chapter released!

mysql advanced (twenty-four) method summary of defense against SQL injection

ROS中编译通过但是遇到可执行文件找不到的问题

C language advanced article: memory function

LeetCode-48. 旋转图像

最牛逼的集群监控系统,它始终位列第一!

Apache APISIX 2.15 版本发布,为插件增加更多灵活性

Matlab学习13-图像处理之可视化GUI程序

TiKV & TiFlash 加速复杂业务查询丨TiFlash 应用实践
随机推荐
hystrix 服务熔断和服务降级
深度学习:文本CNN-textcnn
R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,ncol参数指定行的个数、byrow参数指定按照行顺序排布图
3年软件测试经验,不懂自动化基础...不知道我这种测试人员是不是要被淘汰了?
智能日报脚本
什么是Weex
R语言绘制时间序列的自相关函数图:使用acf函数可视化时间序列数据的自相关系数图
从零开始Blazor Server(6)--基于策略的权限验证
使用.NET简单实现一个Redis的高性能克隆版(一)
矩阵的计算[通俗易懂]
什么是bin文件?「建议收藏」
899. 有序队列 : 最小表示法模板题
"Digital Economy Panorama White Paper" Financial Digital User Chapter released!
【必读要点】Pod控制器Deployment更新、回退详解
随机森林项目实战---气温预测
pytorch+tensorboard使用方法
bash for循环
R语言ggplot2可视化:使用ggpubr包的ggline函数可视化折线图、设置add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图
日常开发写代码原则
bash if conditional judgment