2021SC@SDUSC 项目实训-气象数据获取

2021SC@SDUSC


一、数据来源

根据对相关内容论文的阅读[1] Yu Zheng, Furui Liu, Hsun-Ping Hsie. U-Air: When Urban Air Quality Inference Meets Big Data. 19th SIGKDD conference on Knowledge Discovery and Data Mining (KDD 2013).
[2] Yu Zheng, Xuxu Chen, Qiwei Jin, Yubiao Chen, Xiangyun Qu, Xin Liu, Eric Chang, Wei-Ying Ma, Yong Rui, Weiwei Sun. A Cloud-Based Knowledge Discovery System for Monitoring Fine-Grained Air Quality. MSR-TR-2014-40.
[3]Yu Zheng, Xiuwen Yi, Ming Li, Ruiyuan Li, Zhangqing Shan, Eric Chang, Tianrui Li. Forecasting Fine-Grained Air Quality Based on Big Data. In the Proceeding of the 21th SIGKDD conference on Knowledge Discovery and Data Mining (KDD 2015).
结合实际情况,我计划从多种渠道获取数据来源。初步计划包括但不限于AQI实时数据,天气相关数据(风速,温度,气压,湿度),路网数据(路网密度),poi(Point of Interest兴趣点)。

二、数据的获取

本文介绍气象相关信息的获取,首先要注意到的一个关键问题就是必须要有足够的访问量!!!
否则没办法撑起项目的运行(项目的运行必须需要每小时大量气象数据的获取),在网上多方查找,找到一个比较适合的网站:https://openweathermap.org/api。经过注册成为开发者用户可以提升权限,大概允许访问量如下。还是可以基本满足项目规模需要。
在这里插入图片描述
所需要访问的API接口地址如下https://openweathermap.org/current。感兴趣的可以自己去看,可以返回非常多类型种类的气象数据,粒度也足够细,使用起来应该比较好。
下面附可以批量访问的代码。

from urllib.parse import urlencode
import random
import requests
import traceback
from time import sleep
from lxml import etree
import pandas as pd
import datetime
import json
import json

from AirDeploy import settings
headers = {
    
    
输入自己的请求头,其实没有也行,这种API网址随便跑的

}
points=pd.read_csv('allPos',sep='\t',header=None)#批量给出经纬度

import pandas as pd
data = pd.DataFrame(data=None,columns=['temperature','humidity','windspeed'])
for i in range(len(points)):
    X = str(points.loc[i,0])
    Y = str(points.loc[i,1])
    url1 = 'https://api.openweathermap.org/data/2.5/weather?lat='
    lat = str(Y)
    #print(lat)
    url2 = '&lon='
    lon = str(X)
    url3 = '&appid=你的key'
    url = url1 + lat + url2 + lon + url3
    response = requests.request("POST",url, headers = headers)
    #print(response)
    re = response.content.decode('utf-8')
    jsonData = json.loads(re)
    #print(jsonData)
    data.loc[i] = [jsonData['main']['temp'], jsonData['main']['humidity'],jsonData['wind']['speed']]
data.to_csv('./input/weather.csv',index=False)

这里注意获取到的数据格式其实是json形式的,我们通过json.loads()完成编码就可以轻松通过索引访问。

猜你喜欢

转载自blog.csdn.net/m0_46306466/article/details/123784746