使用新浪微博官方API抓取微博数据(Python版)
2016-10-31 15:27
633 查看
一、安装环境
从网站:http://github.liaoxuefeng.com/sinaweibopy/ 下载安装SDK,
然后就可以看一下网站:https://github.com/michaelliao/sinaweibopy/wiki/OAuth2-HOWTO 的说明
二、一个简单的例子
# coding=utf-8
from weibo import APIClient
import webbrowser # python内置的包
APP_KEY = '你的app_key' # 你的app_key
APP_SECRET = '你的app_secret' # 你的app_secret
CALLBACK_URL = 'http://www.baidu.com' # 网站回调地址
# 在网站设置"使用微博账号登陆"的链接,当用户点击链接后,引导用户跳转至如下地址
# 利用官方微博ADK
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) # 打开了一个网址,网址后面附带了你需要的code
# 用户授权后,将跳转至网站回调地址,并附加参数code=abcd1234
# 获取URL参数code:
print "输入url中code后面的内容后按回车键:"
code = raw_input() # 人工输入网址后面的code内容
r = client.request_access_token(code) # 获得用户授权
# 保存access_token, expires_in
access_token = r.access_token # 新浪返回的token,类似abc123xyz456
expires_in = r.expires_in
# 设置得到的access_token,client可以直接调用API了
client.set_access_token(access_token, expires_in)
# 可以打印下看看里面都是什么东西
# print client.statuses__public_timeline()
statuses = client.statuses__public_timeline()['statuses']
length = len(statuses)
# 输出了部分信息
for i in range(0, length):
print u"微博创建时间:" + statuses[i]['created_at']
print u'昵称:' + statuses[i]['user']['screen_name']
print u'简介:' + statuses[i]['user']['description']
print u'位置:' + statuses[i]['user']['location']
print u'微博:' + statuses[i]['text']
从网站:http://github.liaoxuefeng.com/sinaweibopy/ 下载安装SDK,
然后就可以看一下网站:https://github.com/michaelliao/sinaweibopy/wiki/OAuth2-HOWTO 的说明
二、一个简单的例子
# coding=utf-8
from weibo import APIClient
import webbrowser # python内置的包
APP_KEY = '你的app_key' # 你的app_key
APP_SECRET = '你的app_secret' # 你的app_secret
CALLBACK_URL = 'http://www.baidu.com' # 网站回调地址
# 在网站设置"使用微博账号登陆"的链接,当用户点击链接后,引导用户跳转至如下地址
# 利用官方微博ADK
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) # 打开了一个网址,网址后面附带了你需要的code
# 用户授权后,将跳转至网站回调地址,并附加参数code=abcd1234
# 获取URL参数code:
print "输入url中code后面的内容后按回车键:"
code = raw_input() # 人工输入网址后面的code内容
r = client.request_access_token(code) # 获得用户授权
# 保存access_token, expires_in
access_token = r.access_token # 新浪返回的token,类似abc123xyz456
expires_in = r.expires_in
# 设置得到的access_token,client可以直接调用API了
client.set_access_token(access_token, expires_in)
# 可以打印下看看里面都是什么东西
# print client.statuses__public_timeline()
statuses = client.statuses__public_timeline()['statuses']
length = len(statuses)
# 输出了部分信息
for i in range(0, length):
print u"微博创建时间:" + statuses[i]['created_at']
print u'昵称:' + statuses[i]['user']['screen_name']
print u'简介:' + statuses[i]['user']['description']
print u'位置:' + statuses[i]['user']['location']
print u'微博:' + statuses[i]['text']
相关文章推荐
- python使用新浪微博api上传图片到微博示例
- 分享:Python使用cookielib和urllib2模拟登陆新浪微博并抓取数据
- 使用python抓取知乎日报的API数据
- Python使用新浪微博API发送微博的例子
- 使用微博API(nearby timeline接口)搜集含GPS新浪微博数据
- Python微博地点签到大数据实战 微博API的使用
- 【Scrapy】 使用 python 抓取新浪微博 数据
- python使用新浪微博api上传图片到微博示例
- Python使用新浪微博API发送微博的例子
- Python使用cookielib和urllib2模拟登陆新浪微博并抓取数据
- 使用Python调用Flickr API抓取图片数据
- Python微博地点签到大数据实战(一)微博API的使用
- 新浪微博API使用入门:申请应用、授权、使用官方java版本SDK
- #小练习 使用正则抓取oschina博客专区首页数据 分类: python 小练习 正则表达式 2013-11-11 17:22 604人阅读 评论(0) 收藏
- Python例程:使用adodbapi存取二进制数据
- Python中使用PhantomJS抓取Javascript网页数据
- 在Python3中使用asyncio库进行快速数据抓取的教程
- 使用python调用新浪微博API的小经历
- Python3.2官方文档翻译--使用二进制数据记录布局和多线程
- chencang-----是使用淘宝API获取数据好,还是利用爬虫去抓取数据好呢