python requests练手——和风天气api

练习帖,水一波

最近在学习使用各云平台的api,半桶水的python看的云里雾里的,还是先把python捡回来再继续看吧

 

requests这个轮子很nice,是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到。
和风天气api是随便找的,看前端页面不low就用他,普通用户免费额度是1000次/天,认证开发者的话是14959次/天,练手用的无所谓啦。

#!/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版本的api

转载于:https://my.oschina.net/u/3097874/blog/1572809

你可能感兴趣的:(python,json,爬虫)