Python如何调用新浪api接口的问题
来源:互联网 发布:office办公软件教材 编辑:程序博客网 时间:2024/06/05 20:01
前言:这些天在研究如何调用新浪开放平台的api分析新浪微博用户的数据
成果:成功调用了新浪api获取了用户的一些个人信息和无数条公共微博
不足:新浪开放平台访问有限制,返回的数据着实有限,不足以分析问题,真的要分析问题还是得个人写爬虫
下面是调用新浪开放api的过程:
第一步:按这个做就行
http://www.cnblogs.com/dhsunny/p/3578399.html?utm_source=tuicool&utm_medium=referral
其中有一不那个新浪api测试工具是打不开的,要自己百度
api测试工具打开
第二步:这是是我重点介绍一步,调用新浪api怎么获取数据,总不能在api测试工具上面弄吧,这个时候我采用的是用python
先搭建好python开发环境,具体参考:http://www.imooc.com/learn/397
个人建议在eclipse上面搭建,视频上有教程
下面是调用新浪api的代码:调用的接口是
statuses/public_timeline
这个接口的介绍是:http://open.weibo.com/wiki/2/statuses/public_timeline
代码如下:
#coding:utf-8
from weibo import APIClient
import webbrowser
import MySQLdb
APP_KEY = '984793585' # app key
APP_SECRET = 'ab2c926021d5cfbbc75587e67bd05a8c' # app secret
CALLBACK_URL = 'http://weibo.com/muqingcai/home?wvr=5'# 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("input the code: ").strip()
#新浪返回的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后,可以做任何事情了
#print client.statuses__public_timeline()
count = 1
s = set([('mu','basketball','guangzhou','liuchuanfen')])
def getDataByPublic(count):
while True:
if count>=50:
break
statuses = client.statuses__public_timeline()['statuses']
length = len(statuses)
#输出了部分信息
for i in range(0,length):
nickName = statuses[i]['user']['screen_name']
profile = statuses[i]['user']['description']
location = statuses[i]['user']['location']
weibo = statuses[i]['text']
print u'昵称:'+nickName
print u'简介:'+profile
print u'位置:'+location
print u'微博:'+weibo
count += 1
getDataByPublic(1)
from weibo import APIClient
import webbrowser
import MySQLdb
APP_KEY = '984793585' # app key
APP_SECRET = 'ab2c926021d5cfbbc75587e67bd05a8c' # app secret
CALLBACK_URL = 'http://weibo.com/muqingcai/home?wvr=5'# 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("input the code: ").strip()
#新浪返回的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后,可以做任何事情了
#print client.statuses__public_timeline()
count = 1
s = set([('mu','basketball','guangzhou','liuchuanfen')])
def getDataByPublic(count):
while True:
if count>=50:
break
statuses = client.statuses__public_timeline()['statuses']
length = len(statuses)
#输出了部分信息
for i in range(0,length):
nickName = statuses[i]['user']['screen_name']
profile = statuses[i]['user']['description']
location = statuses[i]['user']['location']
weibo = statuses[i]['text']
print u'昵称:'+nickName
print u'简介:'+profile
print u'位置:'+location
print u'微博:'+weibo
count += 1
getDataByPublic(1)
说明:APP_KEY 和APP_SECRET 需要在新浪开放平台上面创建应用才能获得,具体方法看第一步,CALLBACK_URL也要在应用信息的高级信息里面说明,这是回掉地址,就填你微博的首页地址
运行上面程序:会弹出:
输入你的微博账号密码,接着到了微博主页
把code输入到控制台按回车:
即可获得数据,这些数据可以存在数据库,也可以存到本地
0 0
- Python如何调用新浪api接口的问题
- 如何通过python调用新浪微博的API
- 如何通过python调用新浪微博的API
- Android调用新浪天气API的问题
- python调用新浪API爬虫
- 新浪微博API使用之python接口的使用
- 使用python调用新浪微博API的小经历
- 如何使用.NET调用新浪微博接口的实例
- Android 新浪微盘API调用出现的问题
- python模拟登录新浪微博自动获得调用新浪api所需的code
- python模拟登录新浪微博自动获得调用新浪api所需的code
- python调用新浪微博API
- python 调用 有道api接口
- PHP如何调用API接口
- ASP.NET调用新浪微博开放平台API接口的代码示例
- ASP.NET 调用 新浪微博开放平台API 接口的操作
- ASP.NET调用新浪微博开放平台API接口的代码示例
- 新浪网易IP地区信息查询API接口调用方法
- 蓝桥杯 历届试题 带分数
- win7虚拟机无法上网
- CheckBox OnCheckedChange
- 2016智昂第一个新品即将上市,超越者B3配置表
- 测试新团队如何开展工作
- Python如何调用新浪api接口的问题
- 找不到mysql服务
- hdoj 5480 Conturbatio 【思维】
- #5JAVA字符串StringBuffer 和StringBuilder和 String #
- Android代码资源的国际化
- 使用Ajax递归调用服务器端示例代码
- 用opencv画虚线框矩形
- VC 获取系统时间
- php单例模式应用于mysql数据库操作