抓取武汉市空气质量
程序员文章站
2022-03-03 10:19:35
...
# -*- coding: UTF-8 -*-
__author__ = 'zy'
__time__ = '2020/10/10 19:06'
import requests
import json,time
import sqlite3
def trans(cursor):
wh="https://restapi.amap.com/v3/airquality/aqilist?key=f0f577c0b97d416f142fed74e8bbd3d7&zoom=11&bounds=114.081939%2C30.640691%3B114.619326%2C30.487053&citycode=420100"
response=requests.get(wh)
# print(response.text)
# print(type(response.text))
resp = json.loads(response.text)
# print(resp)
#
# print(resp['datas'])
#
# print(len(resp['datas']))
ids=[]
locations=[]
names=[]
aqis=[]
for i in resp['datas']:
id=i['id']
ids.append(id)
location=i['location']
locations.append(location)
name=i['name']
names.append(name)
aqi=i['aqi']
aqis.append(aqi)
url2="https://restapi.amap.com/v3/airquality/aqidetail?key=f0f577c0b97d416f142fed74e8bbd3d7&ids={station}"
#b'{"status":"1","info":"OK","infocode":"10000","datas":[{"id":"70299","name":"\xe6\xad\xa6\xe6\x98\x8c\xe7\xb4\xab\xe9\x98\xb3","aqi":"65","pm2.5":"32","so2":"13","no2":"28","o3":"107","co":"800","pm10":"80"}]}'
index=0
for i in ids:
print(i)
tmp=url2.format(station=i)
response=requests.get(tmp)
resp = json.loads(response.text)
# "aqi": "65", "pm2.5": "32", "so2": "13", "no2": "28", "o3": "107", "co": "800", "pm10": "80"
aqi=resp['datas'][0]['aqi']
pm2=resp['datas'][0]['pm2.5']
so2=resp['datas'][0]['so2']
no2=resp['datas'][0]['no2']
o3=resp['datas'][0]['o3']
co=resp['datas'][0]['co']
pm10=resp['datas'][0]['pm10']
# 插入数据
#time.time()
sql = "INSERT INTO Air(id,stationname,location,crawl_time,aqi,pm2,so2,no2,o3,co,pm10) VALUES('%s','%s','%s','%s','%f','%f','%f','%f','%f','%f','%f')"%(i,names[index],locations[index],int(time.time()),float(aqis[index]),float(pm2),float(so2),float(no2),float(o3),float(co),float(pm10))
cursor.execute(sql)
conn.commit()
# 提交事物
index = index + 1
if __name__=='__main__':
#id,name,location,aqi,pm2,so2,no2,o3,co,pm10
# 创建表
# sql = 'CREATE TABLE Air(id varchar(30) , ' \
# 'stationname varchar(30), ' \
# 'location varchar(30), crawl_time varchar (30),' \
# 'aqi float (32),pm2 float (32),so2 float (32),no2 float (32),o3 float (32),co float (32),' \
# 'pm10 float (32))'
# cursor.execute(sql)
# time
# trans(cursor)
while 1:
time.sleep(3600)
# 连接数据库(如果不存在则创建)
conn = sqlite3.connect('air.db')
print("Opened database successfully")
...
# 创建游标
cursor = conn.cursor()
try:
trans(cursor)
except Exception as e:
print(e)
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
#jQuery21404026207131369124_1602326816244