Weibo API和Baidu API

/ 2评 / 0

    在上次的学习过程当中,我尝试使用了网易云音乐的API,其中涵盖了不少可用的好方法,而此次我想更进一步,来看查微博和百度的API

WeiBo API

    开局我选择直接查看一下官方提供的API:http://open.weibo.com/wiki/%E9%A6%96%E9%A1%B5 而其内容十分丰富和强大,在权限上微博分为了高级权限接口,需要申请才可以调用需要单独向用户提出SCOPE授权请求的接口,用户单独授权后才可以调用的两种

    微博的验证系统从上面我就看到了,OAuth2.0,以目前水平并不能拿这个验证系统怎么样,还好我找到一个熟人的SDK(廖雪峰大佬提供)。。。没错,我肯定不能像上次网易云那样手动使用,而是用python代码来实现和API的交互。


嫌麻烦也可以日常pip install sinaweibopy 安装完成之后得搞到那个OAuth2.0的授权码。。毕竟拿他没办法。登陆自己的微博打开微博开放平台先填个人信息之后创建一个应用,不用审核过线,只需要拿到app-key和app-secret 然后设置下面这两项:


    将“授权回调页面”设置为:https://api.weibo.com/oauth2/default.html,将“取消授权回调页”也设置为:https://api.weibo.com/oauth2/default.html。

    到此准备工作完成,尝试出第一个程序(源码和博客思路来源于 http://blog.csdn.net/gamer_gyt/article/details/51839159):

from weibo import APIClient
APP_KEY = 'xxxxxxxx'#嗯哼,写你自己的
APP_SECRET = 'xxxxxxxxxx'
CALLBACK_URL = 'https://api.weibo.com/oauth2/default.html'

client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
url = client.get_authorize_url()
print(url)

     这样一来我得到了一个授权页面(页面东西不重要。因为此间过程当中得手动授权,授权后会给授权码=-=不给看):


嗨呀,这下有授权了就能干很多事情了。比如先获取一组微博吧:

code = "xxxxxxxxxxxxxx"#上面图片页面会有一个code=...
r = client.request_access_token(code)
access_token = r.access_token
expires_in = r.expires_in
client.set_access_token(access_token, expires_in)
statuses = client.statuses__friends_timeline()['statuses']
lens = len(statuses)
for i in range(0,lens):
    print (u'name:'+statuses[i]['user']['screen_name'])
    print (u'art:' + statuses[i]['user']['description'])
    print (u'loca:' + statuses[i]['user']['location'])
    print (u'row:' + statuses[i]['text'])
    print ("--------------------------------------")

     

    开微博实地验证:

(授权代码过几分钟就会过期。上面程序可没续一秒的用法,和之前一样:是JSON<huaJi>)

Baidu API

    百度应该能为中国用户提供最大、最好用的API套件的公司,我在百度搜索引擎下找到的API几乎涵盖了很多的网络的功能,而其为开发者也形成了良好的市场(有个APIStore或许我的理解是错的。这里我拿着百度地图来试试#毕竟免费我也很无奈啊)

    百度地图API需要自己去申请Key。当这一切准备工作做完之后:

    设置好IP白名单:(我这里因为不是开发我不做限制直接0.0.0.0/0),保存

    http://lbsyun.baidu.com/index.php?title=webapi

    自己查看API发现一堆东西。。。恐怖如斯

    测试一下:


    根据API描述来看 https://api.map.baidu.com/geocoder/v2/?ak=不给看的KEy密钥&callback=renderReverse&location=39.915136,116.404035&output=xml&pois=1,输出为xml(虽然坐标精确还是太大了。。我换个小点的地方===):


    也可以输出为JSON格式(强行JSON)而这里我也要强行JSON(因为我他喵的没有下载xml解析工具啊。#说的好像你有JSON解析工具似的。)毕竟JSON解析猛如狗。

来解析啊=-=

    json是python标准库的一部分,无需安装(xml:???),只要轻轻import json即可使用,不同与其他的方法,python不需要在解析前把json解析为json对象或者json节点,python更是生猛一点:把json转换成字典,json数组转换为列表,json字符串转换成python字符串,这样就很是简单了。

    经过对上面的结果观察狗:我把所有地址提取出来

# coding=utf-8
import json
JsonString = '{"status":0,"result":{"location":{"lng":116.40403499999992,"lat":39.91513607702646},"formatted_address":"北京市东城区中华路甲10号","business":"天安门,前门,和平门","addressComponent":{"country":"中国","country_code":0,"province":"北京市","city":"北京市","district":"东城区","adcode":"110101","street":"中华路","street_number":"甲10号","direction":"附近","distance":"46"},"pois":[{"addr":"北京市东城区东长安街","cp":"","direction":"内","distance":"0","name":"天安门","poiType":"旅游景点","point":{"x":116.40395508249037,"y":39.915043201105998},"tag":"旅游景点","tel":"","uid":"65e1ee886c885190f60e77ff","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"北京市东城区东长安街天安门内","cp":" ","direction":"东北","distance":"117","name":"天安门-观礼台","poiType":"旅游景点","point":{"x":116.40316457364159,"y":39.91467656754177},"tag":"旅游景点;风景区","tel":"","uid":"d5a70b82114e2b2bddee8bae","zip":"","parent_poi":{"name":"天安门","tag":"旅游景点;风景区","addr":"北京市东城区东长安街","point":{"x":116.40395508249037,"y":39.91511237702834},"direction":"附近","distance":"9","uid":"65e1ee886c885190f60e77ff"}},{"addr":"北京市东城区东长安街天安门内","cp":" ","direction":"东北","distance":"136","name":"天安门-前石狮子","poiType":"自然地物","point":{"x":116.40338915001908,"y":39.91433068500728},"tag":"自然地物;山峰","tel":"","uid":"25a0088af2e3b35cd8ecedf6","zip":"","parent_poi":{"name":"天安门","tag":"旅游景点;风景区","addr":"北京市东城区东长安街","point":{"x":116.40395508249037,"y":39.91511237702834},"direction":"附近","distance":"9","uid":"65e1ee886c885190f60e77ff"}},{"addr":"北京市东城区东长安街天安门内","cp":" ","direction":"东北","distance":"145","name":"天安门-华表","poiType":"旅游景点","point":{"x":116.40329931946809,"y":39.91430301432845},"tag":"旅游景点;风景区","tel":"","uid":"3ff8be14bf465f3682288ac1","zip":"","parent_poi":{"name":"天安门","tag":"旅游景点;风景区","addr":"北京市东城区东长安街","point":{"x":116.40395508249037,"y":39.91511237702834},"direction":"附近","distance":"9","uid":"65e1ee886c885190f60e77ff"}},{"addr":"北京市东城区","cp":" ","direction":"东北","distance":"154","name":"长安街","poiType":"旅游景点","point":{"x":116.40339813307418,"y":39.914185413817758},"tag":"旅游景点;风景区","tel":"","uid":"ef7d3ba151d502cdc61b759e","zip":"","parent_poi":{"name":"天安门","tag":"旅游景点;风景区","addr":"北京市东城区东长安街","point":{"x":116.40395508249037,"y":39.91511237702834},"direction":"附近","distance":"9","uid":"65e1ee886c885190f60e77ff"}},{"addr":"中华路4号中山公园","cp":" ","direction":"东","distance":"206","name":"中山公园-南门","poiType":"出入口","point":{"x":116.40222135285612,"y":39.91483567291639},"tag":"出入口;门","tel":"","uid":"ffd3f23ff3ef0af7363a713f","zip":"","parent_poi":{"name":"中山公园","tag":"旅游景点;文物古迹","addr":"东城区中华路4号(天安门西侧)","point":{"x":116.40057745377286,"y":39.91669648637688},"direction":"东南","distance":"446","uid":"e2c734e489d8d15d15a190d1"}},{"addr":"北京市东城区景山前街4号天安门广场内","cp":" ","direction":"北","distance":"269","name":"天安门广场-国旗","poiType":"旅游景点","point":{"x":116.40392813332507,"y":39.91327227351458},"tag":"旅游景点;风景区","tel":"","uid":"4ae2adcf574bcd2b38221c66","zip":"","parent_poi":{"name":"天安门广场","tag":"旅游景点;风景区","addr":"北京市东城区东长安街","point":{"x":116.40416169275767,"y":39.909674934750039},"direction":"北","distance":"789","uid":"c9b5fb91d49345bc5d0d0262"}},{"addr":"公园南门内东侧文化宫院内","cp":" ","direction":"西","distance":"190","name":"文化宫派出所","poiType":"政府机构","point":{"x":116.40572474434502,"y":39.914911766659518},"tag":"政府机构;公检法机构","tel":"","uid":"4ce10ce6f6e206a1441a32d1","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"西长安街附近","cp":" ","direction":"东","distance":"408","name":"水榭","poiType":"房地产","point":{"x":116.40038880961575,"y":39.91484259053293},"tag":"房地产;住宅区","tel":"","uid":"4afdeac16b71ec421fe99904","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"北京市东城区劳动人民文化宫西门","cp":" ","direction":"南","distance":"593","name":"鑫溢宫食府","poiType":"美食","point":{"x":116.40461982856775,"y":39.91921438330369},"tag":"美食;中餐厅","tel":"","uid":"7bfa08fc50423f6625d2bb1f","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"南","distance":"1296","uid":"06d2dffda107b0ef89f15db6"}}],"roads":[],"poiRegions":[{"direction_desc":"内","name":"天安门","tag":"旅游景点"}],"sematic_description":"天安门内","cityCode":131}}'
jsonObj = json.loads(JsonString)
leng = len(jsonObj.get("result").get("pois"))
for i in range(0,leng):
    print(jsonObj.get("result").get("pois")[i].get("addr"))

    地图API强的一批,详情还希望去百度看看官方文档,溜了(今天查资料遭遇了秀恩爱。。卧槽我也想要女朋友啊。#Vt神:这么菜找什么女盆友,先点菜吧)

  1. kemu说道:

    很喜欢您的文章谢谢你们

  2. jiaoyu说道:

    希望看到更多优秀文章多谢.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注