練習帖,水一波前端
最近在學習使用各雲平臺的api,半桶水的python看的雲裏霧裏的,仍是先把python撿回來再繼續看吧python
requests這個輪子很nice,是一個很實用的Python HTTP客戶端庫,編寫爬蟲和測試服務器響應數據時常常會用到。
和風天氣api是隨便找的,看前端頁面不low就用他,普通用戶免費額度是1000次/天,認證開發者的話是14959次/天,練手用的無所謂啦。json
#!/usr/bin/env python #coding=utf-8 import requests import json yburl = 'https://free-api.heweather.com/s6/weather/forecast' cyurl = 'https://free-api.heweather.com/s6/weather/lifestyle' value = { 'location':'廣州', 'key':'1234567890123456789', 'lang':'zh' } ybreq = requests.get(yburl,params=value) cyreq = requests.get(cyurl,params=value) ybjs = ybreq.json() cyjs = cyreq.json() for i in range(2): yb = ybjs['HeWeather6'][0]['daily_forecast'] cy = cyjs['HeWeather6'][0]['lifestyle'][1] gj = cyjs['HeWeather6'][0]['lifestyle'][0] d1 = u'廣州' + ' ' + yb[i]['date'] + ' ' + yb[i]['cond_txt_d'] d2 = gj['txt'] + ' ' + cy['txt'] d3 = d1 + '\n' + d2 print d3.encode('GBK')
PS:練手用的是和風天氣的s6版本的apiapi