Python获取2021疫情春运防疫政策查询(聚合数据接口)

https://www.juhe.cn/docs/api/id/566

Python获取2021疫情春运防疫政策查询(聚合数据接口)

用的是聚合数据的这个接口,自己摸索的。。写的不太好,有可以优化的地方请在评论区指出,谢谢

1.首先建表及获取省份城市信息

import requests
import pymssql
import json

url = "http://apis.juhe.cn/springTravel/citys?key=XXXXXXXXXXXXXXXXXXXX"
data = {"city_id":"city"}
res = requests.post(url=url,data=data)
'''接口获取疫情省份城市id信息,key需要自己在聚合数据上获取(该接口普通账户每天能查询100次)'''
db1 = pymssql.connect('XXXXXXXXXXX', 'XXXXXXXXXXXX', 'XXXXXXXX', 'XXXXXXXXXX')

cur2 = db1.cursor()
cur2.execute('drop table if exists FY_PY_CITYS')

sql1 = """CREATE TABLE [dbo].[FY_PY_CITYS] (
[id] int NOT NULL IDENTITY(1,1) ,
[province_id] int NOT NULL ,
[province] nvarchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[city_id] int NOT NULL ,
[city] nvarchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[high_in_desc] nvarchar(MAX) COLLATE Chinese_PRC_CI_AS NULL ,
[low_in_desc] nvarchar(MAX) COLLATE Chinese_PRC_CI_AS NULL ,
[out_desc] nvarchar(MAX) COLLATE Chinese_PRC_CI_AS NULL ,
[CreateDatetime] datetime NOT NULL DEFAULT (getdate()) 
)"""

cur2.execute(sql1)

db1.commit()
i=-1
data = json.loads(res.content)
for province in  data['result']:
    i=i+1
    for city in  data['result'][i]['citys']:
        _province_id=str(i+1)
        _province=province['province']
        _city_id=city['city_id']
        _city=city['city']
        query = """insert into FY_PY_CITYS (province_id, province, city_id,city)  values 
        ('"""+_province_id+"""','"""+_province+"""','"""+_city_id+"""','"""+_city+"""')
         """
        cur2.execute(query)
        db1.commit()
        print(str(i+1)+'-'+province['province']+'_'+city['city']+'-'+city['city_id'])

2.接口里的信息我这边只需要进出省份城市的防疫政策,仅作参考(360个城市,每天限100次查询,所以如果要每天更新的话,需要两个账号的key,同样的写法改一下i的值就可以了。。。贫穷)

import requests
import pymssql
import json
import time

db1 = pymssql.connect('XXXXXXXXXXX', 'XXXXXXXXXXXX', 'XXXXXXXX', 'XXXXXXXXXX')
cur2 = db1.cursor()

i=-1
for i in range(90) :
    i=i+1
    from_='select city_id from FY_PY_CITYS where id='+str(i)
    to_='select city_id from FY_PY_CITYS where id='+str(i+200)
    from_=cur2.execute(from_)
    from_=cur2.fetchone()
    print("from_ : %s " %from_)
    to_=cur2.execute(to_)
    to_=cur2.fetchone()
    print("to_ : %s " %to_)
    url = "http://apis.juhe.cn/springTravel/query?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX&from={}&to={}".format(from_[0], to_[0])
    
    res = requests.get(url=url)
    '''接口获取疫情省份城市防疫政策'''
    res.encoding = 'utf-8'
    data = json.loads(res.content)
    from_high_in_desc=data['result']['from_info']['high_in_desc']
    from_low_in_desc=data['result']['from_info']['low_in_desc']
    from_out_desc=data['result']['from_info']['out_desc']
    
    to_high_in_desc=data['result']['to_info']['high_in_desc']
    to_low_in_desc=data['result']['to_info']['low_in_desc']
    to_out_desc=data['result']['to_info']['out_desc']
    
    from_query = """update FY_PY_CITYS 
    set high_in_desc='{0}', low_in_desc='{1}',out_desc='{2}'
    where city_id={3}""".format(from_high_in_desc,from_low_in_desc,from_out_desc,from_[0])    
    cur2.execute(from_query)
    
    time.sleep(1)
    
    to_query = """update FY_PY_CITYS 
    set high_in_desc='{0}', low_in_desc='{1}',out_desc='{2}'
    where city_id={3}""".format(to_high_in_desc,to_low_in_desc,to_out_desc,to_[0])    
    cur2.execute(to_query)
    
    db1.commit()
    print(str(i)+':\n from_high_in:'+from_high_in_desc+'\nfrom_low_in:'+from_low_in_desc+'\nfrom_out:'+from_out_desc
          +':\n to_high_in:'+to_high_in_desc+'\nto_low_in:'+to_low_in_desc+'\nto_out:'+to_out_desc)

3.大致成果

Python获取2021疫情春运防疫政策查询(聚合数据接口)

上一篇:Git--Git分支详解与用法


下一篇:git - 简明指南 助你入门 git 的简明指南,木有高深内容