网站地址
视频教程
python代码
# -*- coding: utf-8 -*-
# @Author: Null119 微信公众号/网站:治廷君
# @Desc: { 台州气象数据获取 }
# @Date: { 2022/7/5 }
import csv,requests,re
from datetime import datetime
r=requests.Session()
varName=['温度','降水','平均风速','最大阵风','最大平均风速','相对温度','海平面气压','本站气压','能见度','近24小时能见度']
varId=['T','Pr','Wind10','WindExMax','WindMax','Rh','Slp','P','Vis','VisMin']
def write_csv(contents,name,typeid):
csvname=f'{name}-{varName[typeid]}-data-{datetime.now().strftime("%Y%m%d%H%M%S")}.csv'
with open(f'./{csvname}','w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(['datatime',varId[typeid]])
writer.writerows(contents)
print(csvname,'写入完成.')
def get_time():
GMT = '%a %b %d %Y %H:%M:%S GMT+0800'
nowtime=datetime.now()
return nowtime.strftime(GMT)+' (中国标准时间)',nowtime.strftime("%Y-%m-%d %H:%M:%S")
def get_token():
response=r.get('http://www.tz121.com/index.php/Observation')
try:
token=re.search(r'name="_token" value="(.*?)"',response.text).group(1)
except:
token=''
setCookieContent=response.headers['Set-Cookie']
XTOKEN =re.search('(XSRF-TOKEN=.*?);',setCookieContent).group(1)
taizhou_session =re.search('(taizhou_session=.*?);',setCookieContent).group(1)
cookie=f'{XTOKEN};{taizhou_session};'
return token,cookie
def get_data(name,day,typeid):
token,cookie=get_token()
dtStart,dtEnd=get_time()
postdata={
"varId":varId[typeid],
"dtEnd":dtEnd,
"dtStart":dtStart,
"timeUnit":"H",
"timeSpan":"0",
"timeStep":"1",
"timeFixEnd":"-1",
"sqlFunc":"",
"county":"台州"
}
headers={
"Accept":"*/*",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Cookie":cookie,
"Referer":"http://www.tz121.com/index.php/Observation",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37",
"X-CSRF-Token":token,
"X-Requested-With":"XMLHttpRequest"
}
response=r.post('http://www.tz121.com/index.php/Observation/PostObsData',data=postdata,headers=headers)
data=response.json()['data']
id=''
for i in range(len(data)):
if name==data[i]['name']:
id= data[i]['id']
break
if id!='':
postdata={
"stid":id,
"varId":varId[typeid],
"dtEnd":datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"sqlFunc":"",
"hourSpan":"0",
"daySpan":day,
"timeFixEnd":"-1"
}
response=r.post('http://www.tz121.com/index.php/Observation/PostObsDataList',data=postdata,headers=headers)
data=response.json()['data']
rdata=[]
for i in range(len(data)):
rdata.append([data[i]['t'],data[i]['v1']])
print(data[i]['t'],data[i]['v1'])
write_csv(rdata,name,typeid)
else:
print('没有找到对应id,请检查后再试.')
if __name__ == '__main__':
for i in range(len(varName)):
print(i,varName[i])
type=input('输入要获取的数据类型:')
#点位名称,获取天数,获取类型
get_data('天台','1',int(type))
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习使用,请在下载后24小时内删除,严禁商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
【注意:本站发布资源来源于网络搜集,均有较强时效性,请在下载前注意查看文章资源发布或更新时间,距离当前时间太久的资源不建议下载,特别是安卓专区相关资源,会有大概率失效无法使用】
评论(0)