天气、快递API调用
import json
import requests
import time
class WithJson():
def weather(self):
str_json = self.WeatherAPI()
string = str_json.replace("'", '"')#替换"'", '"'
pop_data = json.loads(string)#转python字典
#pop_data = json.load(f)
list_pop_data=pop_data['data']['forecast']#读取列表
#print(pop_data['status'])
#print(pop_data['desc'])
city1=(pop_data['data']['city'])
wendu1=(pop_data['data']['wendu'])
print('\n')
print("今日:"+list_pop_data[0]['date'] +" " +city1+" "+wendu1+"°"+list_pop_data[0]['type'])
print(pop_data['data']['ganmao'])
rq=(pop_data['data']['yesterday']['date'])
tq=(pop_data['data']['yesterday']['type'])
print("*************************************************")
print(rq + ": " + tq)
for pop_dict in list_pop_data:
date1 = pop_dict['date']
tianqi = pop_dict['type']
print(date1 + ": " + tianqi)
def ExpressCheck(self):
str_json =self.ExpressAPI()#字符串
string = str_json.replace("'", '"')#替换"'", '"'
pop_data = json.loads(string)#转python字典
list_pop_data=pop_data['data']#读取列表
#print("访问结果:"+pop_data['message'])
print("快递公司:"+pop_data['com'])
print("快递单号:"+pop_data['nu'])
print("*************************************************")
for pop_dict in list_pop_data:
time1 = pop_dict['time']
context1 = pop_dict['context']
print(time1 + ": " + context1)
def ExpressAPI(self):
URL2 = 'http://www.kuaidi100.com/query?type=zhongtong&postid=73116039505988'
try:
r = requests.get(URL2, params={'ip': '8.8.8.8'}, timeout=1)
r.raise_for_status() # 如果响应状态码不是 200,就主动抛出异常
except requests.RequestException as e:
print(e)
else:
result = r.json()
#print(result)
js = json.dumps(result)
return js
def WeatherAPI(self):
URL2 = 'http://wthrcdn.etouch.cn/weather_mini?city=西安'
try:
r = requests.get(URL2, params={'ip': '8.8.8.8'}, timeout=1)
r.raise_for_status() # 如果响应状态码不是 200,就主动抛出异常
except requests.RequestException as e:
print(e)
else:
result = r.json()
#print(result)
js = json.dumps(result)
#print(js)
return js
if __name__ == '__main__':
WithJson=WithJson()
WithJson.weather()
print("*************************************************")
print('\n')
print("*************************************************")
WithJson.ExpressCheck()
input('Press Enter to exit...')