当前位置:网站首页>如何免费获得一个市全年的气象数据?降雨量气温湿度太阳辐射等等数据
如何免费获得一个市全年的气象数据?降雨量气温湿度太阳辐射等等数据
2022-08-03 12:02:00 【地理遥感生态网】
气象数据一直是一个价值较高的数据,它被广泛用于各个领域的研究当中。气象数据包括有气温、气压、相对湿度、降水、蒸发、风向风速、日照等多种指标,但是包含了这些全部指标的气象数据却较难获取,即使获取到了也不能随意分享。
想要大规模爬取的话,需要自己写爬虫,我之前写过一个爬取深圳市数据的爬虫。对深圳市的天气数据爬取基本没有问题。
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'))对 20210401的深圳天气数据爬取获得的 csv 文件如下图所示:
当然啦,需求量比较大的话,可以通过地理遥感生态网平台获取气象数据。
地理遥感生态网平台发布的气象数据包括有气温、气压、相对湿度、降水、蒸发、风向风速、日照太阳辐射等等多种指标。
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地温)
赶紧三连关注下, 数据获取途径如下:

边栏推荐
- c语言进阶篇:内存函数
- FR9811S6 SOT-23-6 23V, 2A Synchronous Step-Down DC/DC Converter
- 一个扛住 100 亿次请求的红包系统,写得太好了!!
- Objective - C code analysis of the deep and shallow copy
- 【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符
- 微信小程序获取手机号
- How to do App Automation Testing?Practical sharing of the whole process of App automation testing
- 进程内存
- 面试突击71:GET 和 POST 有什么区别?
- 解决oracle安装在linux中jdk的冲突
猜你喜欢

【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符

广州番禺:暑期防溺水,安全不放假

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

码率vs.分辨率,哪一个更重要?
【一起学Rust】Rust学习前准备——注释和格式化输出

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

笔试题:金额拆分

CDH6.3.2开启kerberos认证

Explain the virtual machine in detail!JD.com produced HotSpot VM source code analysis notes (with complete source code)
【一起学Rust】Rust的Hello Rust详细解析
随机推荐
LeetCode-1796. 字符串中第二大的数字
"Digital Economy Panorama White Paper" Financial Digital User Chapter released!
[深入浅出]三位数排序
深入理解MySQL事务MVCC的核心概念以及底层原理
永寿 永寿农特产品-苹果
Traceback (most recent call last): File
ThreadLocal源码解析及使用场景
小身材有大作用——光模块寿命分析(二)
LeetCode 899 Ordered queue [lexicographical order] HERODING's LeetCode road
From scratch Blazor Server (6) - authentication based on strategy
肝完Alibaba这份面试通关宝典,我成功拿下今年第15个Offer
[Wrong title] Circuit maintenance
html+css+php+mysql实现注册+登录+修改密码(附完整代码)
用C语言解决A+B问题,A-B问题,A*B问题
Matlab学习12-图像处理之图像增强
什么是Weex
I in mother's womb SOLO20 years
5个超好用手机开源自动化工具,哪个适合你?
当前页面的脚本发生错误如何解决_电脑出现当前页面脚本错误怎么办
字节最爱问的智力题,你会几道?