import requests
appcode = 'Your AppCode here'
areaid = "101291401"
month = "201805"
url = 'https://ali-weather.showapi.com/weatherhistory'
payload = {'areaid': areaid, 'month': month}
headers = {'Authorization': 'APPCODE {}'.format(appcode)}
r = requests.get(url, params=payload, headers=headers)
r
r.content
import json
content_json = json.loads(r.content)
content_json
content_json['showapi_res_body']['list']
import pandas as pd
df = pd.DataFrame(content_json['showapi_res_body']['list'])
df
def get_df(areaid, areaname_dict, month, appcode):
url = 'https://ali-weather.showapi.com/weatherhistory'
payload = {'areaid': areaid, 'month': month}
headers = {'Authorization': 'APPCODE {}'.format(appcode)}
r = requests.get(url, params=payload, headers=headers)
content_json = json.loads(r.content)
df = pd.DataFrame(content_json['showapi_res_body']['list'])
df['areaname'] = areaname_dict[areaid]
return df
def get_dfs(areaname_dict, months, appcode):
dfs = []
for areaid in areaname_dict:
dfs_times = []
for month in months:
temp_df = get_df(areaid, areaname_dict, month, appcode)
dfs_times.append(temp_df)
area_df = pd.concat(dfs_times)
dfs.append(area_df)
return dfs
areaname_dict = {"101291401":"丽江"}
months = ["201805"]
dfs = get_dfs(areaname_dict, months, appcode)
dfs
dfs[0]
areaname_dict = {"101030100":"天津", "101020100":"上海", "101291401":"丽江"}
months = ["201801", "201802", "201803", "201804", "201805"]
dfs = get_dfs(areaname_dict, months, appcode)
dfs
df = pd.concat(dfs)
df
df.dtypes
df.time = pd.to_datetime(df.time)
df.aqi = pd.to_numeric(df.aqi)
df.dtypes
import matplotlib.pyplot as plt
%matplotlib inline
from plotnine import *
from mizani.breaks import date_breaks
(ggplot(df, aes(x='time', y='aqi', color='factor(areaname)')) + geom_line() +
scale_x_datetime(breaks=date_breaks('2 weeks')) +
xlab('日期') +
theme_matplotlib() +
theme(axis_text_x=element_text(rotation=45, hjust=1)) +
theme(text=element_text(family='WenQuanYi Micro Hei'))
)