引言: Weibo做为中文世界中的媒体霸主,提供了丰富的API供各位开发者使用,这里将基于Python SDK来访问weibo的接口进行示例。
1. Weibo API的介绍
文档地址: http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
前提条件: 注册微博账号,开发开发模式
在微博 API之外,其实Weibo还有非常庞杂的使用场景,大家可以在其Open平台上自行阅读
2. Weibo的Python SDK
其官方并未提供Python的SDK,是由网友提供的(@廖雪峰)
访问地址: http://github.liaoxuefeng.com/sinaweibopy/
其安装非常简单,可以利用Python的PIP直接进行安装, 也可以基于源码来安装。
pip install sinaweibopy
3. 访问Weibo的代码示例
这里的代码访问功能关注的Weibo账户信息,并统计各个地区的关注对象分布,一共使用了3个API:
- 获取国家信息 http://open.weibo.com/wiki/2/common/get_country
- 获取省市信息 http://open.weibo.com/wiki/2/common/get_province
- 获取关注对象信息 http://open.weibo.com/wiki/2/friendships/friends
说明: 在关注对象中,有若干种情况,比如双向关注等,这里仅仅列举一种情况,其余的情况可以参照API使用,不在一一列举。
代码:
# -*- coding: utf-8 -*-
from weibo import APIClient
import webbrowser#python内置的包
APP_KEY = 'App Key'
APP_SECRET = '用户的app secret'
CALLBACK_URL = 'http://www.xxx.top/weibo'
#利用官方微博SDK
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
#得到授权页面的url,利用webbrowser打开这个url
url = client.get_authorize_url()
print url
webbrowser.open_new(url)
# Get the valid code from the browser, 这里在登陆之后,获取到的code,不清楚如何获取,可以参照我的上一篇blog
code = '399f66623d8e154b5fce40df21cd49b6'
#获取token
r = client.request_access_token(code)
access_token = r.access_token # 新浪返回的token,类似abc123xyz456
expires_in = r.expires_in # token过期的UNIX时间
# 设置得到的access_token
client.set_access_token(access_token, expires_in)
#################################
## define the basic funciton
####################################################
def parse_user(users):
print("trying parsing obj reference...")
for user in users:
provincestr = check_province_code(user['province'], "001")
print("provincestr: %s with %s" % (provincestr,str(provincestr in statis_result)))
if provincestr in statis_result:
statis_result[provincestr] = statis_result[provincestr]+1
else:
statis_result[provincestr] = 1
def check_province_code(province, country):
num = len(province)
while num <3:
province = ''.join(['0',province])
num = num +1
return country + province
######## init the basic province/country
countrys = {}
## --- get country code and name
countrylist = client.common.get_country.get()
for country in countrylist:
for (code, name) in country.items():
countrys[code] = name
print "code/name:" + code + "/" + name
provinces = {}
### --- get province
provincelist = client.common.get_province.get(country="001", count=1000)
for province in provincelist:
for (code, name) in province.items():
provinces[code] = name
print "code/name in province:" + code + "/" + name
my_uid = u'1452797454'
statis_result = {}
next_cursor = 0
objRefs = client.friendships.friends.get(uid=my_uid, cursor=next_cursor, count=10)
parse_user(objRefs.users)
next_cursor = objRefs.next_cursor
# next_cursor: 基于cursor来实现循环遍历所有的user元素
while(next_cursor>0):
objRefs = client.friendships.friends.get(uid=my_uid, cursor=next_cursor)
# users array
users = objRefs.users
parse_user(users)
next_cursor = objRefs.next_cursor
for (key, num) in statis_result.items():
if key in provinces:
print("%s has %s friends" %(provinces[key], num))
else:
print("%s (not matched) has %s friends" %(key, num))
for (key, num) in statis_result.items():
print("%s has %s friends" %(provinces[key], num))
运行的结果信息:
4. 总结
大家可以参照sinaweibopy中的文档和代码示例多多学习,接口设计还是非常给力的。