基于Weibo的Python SDK来访问Weibo账号信息
2016-10-21 15:41
357 查看
引言: 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 sinaweibopy3. 访问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中的文档和代码示例多多学习,接口设计还是非常给力的。
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 sinaweibopy3. 访问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中的文档和代码示例多多学习,接口设计还是非常给力的。
相关文章推荐
- Kivy A to Z -- 如何从Python创建一个基于Binder的Service及如何从Java访问Python创建的Service
- Python 获取URL访问的HEAD头信息
- python以post方式登录csdn网站,并以cookie方式访问个人信息
- python基于selenium+PhantomJS实现用提交表单,并点击按钮功能,获取返回的页面源码和ur等信息l。
- python sdk 获取新浪微博签到信息
- 《基于apache 访问账号认证机制》
- 6.Selenium2 自动化测试实战-基于Python语言-126邮箱登陆、获得验证信息
- Python基于HiRedis访问Redis
- 在为应用集成微博第三方登录获取用户信息的时候出现异常com.weibo.sdk.android.WeiboException: {"error":"User does not exists!","er
- 基于Django1.10的python2信息管理系统,用于酒店预订管理
- 基于sina微博官方sdk源码实现的微博授权登录和分享,以及获取信息
- python基于nginx访问日志统计客户端ip访问量
- Python基于nginx访问日志并统计IP访问量
- Redis基础教程第5节 远程访问 Redis 基于 C#/Python
- psutil一个基于python的跨平台系统信息跟踪模块
- python访问的时候,将debug信息打印出来
- Python基于nginx访问日志并统计IP访问量 推荐
- 基于七牛Python SDK写的一个同步脚本
- Python开发【项目】:生产环境下实时统计网站访问日志信息
- 基于高德sdk实现摩拜单车主界面,滑动地图获取地址信息