使用python进行新浪微博应用开发
如何在新浪开放平台上创建一个应用?
- 在开放平台-我的应用下面创建新的应用。按照提示一步一步创建,傻瓜式的。
- 点击刚才创建的应用进入详细页面,然后查看应用信息-基本信息下面。在程序开发过程中,我们需要app key 和 app secret来调用新浪API。
3. 下载对应语言的sdk,当然这里以python为例。下载地址:http://code.google.com/p/sinaweibopy/。下载完成后将里面的weibo.py复制到你的应用程序同一目录下,或者复制到lib/site-package下。这样你的应用就可以调用sdk了。
4. 在你的程序里面做如下测试,如果你幸运的话你应该能得到正确的返回结果了。
from weibo import APIClient
APP_KEY = 'xxxx' # app key
APP_SECRET = 'xxxx' # app secret
CALLBACK_URL = 'xxxxxx'# callback url
#利用官方微博SDK
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
#用得到的url到新浪页面访问
url = client.get_authorize_url()
webbrowser.open_new(url)
#手动输入新浪返回的code
code = raw_input()
#新浪返回的token,类似abc123xyz456,每天的token不一样
r = client.request_access_token(code)
access_token = r.access_token
expires_in = r.expires_in # token过期的UNIX时间
#设置得到的access_token
client.set_access_token(access_token, expires_in)
#有了access_token后,可以做任何事情了
client.get.friendships__followers()
APP_KEY = 'xxxx' # app key
APP_SECRET = 'xxxx' # app secret
CALLBACK_URL = 'xxxxxx'# callback url
#利用官方微博SDK
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
#用得到的url到新浪页面访问
url = client.get_authorize_url()
webbrowser.open_new(url)
#手动输入新浪返回的code
code = raw_input()
#新浪返回的token,类似abc123xyz456,每天的token不一样
r = client.request_access_token(code)
access_token = r.access_token
expires_in = r.expires_in # token过期的UNIX时间
#设置得到的access_token
client.set_access_token(access_token, expires_in)
#有了access_token后,可以做任何事情了
client.get.friendships__followers()
5. 利用API做你能想到的任何事情,比如我做了一个粉丝和关注者的性别分析:

要实现这个统计很简单,首先获得某个用户的所有关注者:
def GetAllFriends(self,uid):
"""
得到所有的关注对象
返回:(screenName,gender)元组数组
"""
resFollows = []
nextCursor = -1
while nextCursor != 0:
followers = self.client.get.friendships__friends(uid=uid,count=200,cursor=nextCursor)
nextCursor = followers["next_cursor"]
for follower in followers["users"]:
resFollows.append( (follower["screen_name"],follower["gender"]) )
print len(resFollows)
return resFollows
"""
得到所有的关注对象
返回:(screenName,gender)元组数组
"""
resFollows = []
nextCursor = -1
while nextCursor != 0:
followers = self.client.get.friendships__friends(uid=uid,count=200,cursor=nextCursor)
nextCursor = followers["next_cursor"]
for follower in followers["users"]:
resFollows.append( (follower["screen_name"],follower["gender"]) )
print len(resFollows)
return resFollows
然后利用matplotlib这个第三方库进行绘图即可。关于matplotlib我想我后面会写一些文章进行说明的,很强大的一个二维绘图库。
def FriendsMaleOrFemale(uid):
wb = MySinaWeiBo()
m = 0
f = 0
n = 0
for i in wb.GetAllFriends(uid):
if i[1] == "m":
m = m+1
elif i[1] == "f":
f = f+1
else:
n = n+1
ind = np.arange(1,4) # np.arange(1,N+1) # the x locations for the groups
width = 0.25 # the width of the bars
plt.subplot(111)
rects1 = plt.bar(ind, (m,f,n), width,bottom = 0,align = 'center')
#增加Y轴说明
plt.ylabel(u'关注数')
#增加标题
plt.title(u'我关注的人性别分析(有效样本:%d)' % (m+f+n))
#设置x坐标位置和文字
plt.xticks(ind, (u"男",u"女",u"未知") )
autolabel(rects1)
plt.legend(rects1,(u"用户:%s" % wb.GetUserByUid(uid),))
plt.show()
wb = MySinaWeiBo()
m = 0
f = 0
n = 0
for i in wb.GetAllFriends(uid):
if i[1] == "m":
m = m+1
elif i[1] == "f":
f = f+1
else:
n = n+1
ind = np.arange(1,4) # np.arange(1,N+1) # the x locations for the groups
width = 0.25 # the width of the bars
plt.subplot(111)
rects1 = plt.bar(ind, (m,f,n), width,bottom = 0,align = 'center')
#增加Y轴说明
plt.ylabel(u'关注数')
#增加标题
plt.title(u'我关注的人性别分析(有效样本:%d)' % (m+f+n))
#设置x坐标位置和文字
plt.xticks(ind, (u"男",u"女",u"未知") )
autolabel(rects1)
plt.legend(rects1,(u"用户:%s" % wb.GetUserByUid(uid),))
plt.show()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器