twitter爬虫

帐号准备

  • 首先需要注册twitter账号,注册页使用google邮箱,下一页验证手机号。
  • 新建一个twitter application 链接:新建项目
    按要求填写内容后在keys and access token页面获取对应keys。

环境配置

安装tweepy:

1
pip install tweepy

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#  -*- coding: utf-8 -*-
import tweepy
from tweepy import OAuthHandler

consumer_key = 'replace your own account consumer_key'
consumer_secret = 'replace your own account consumer_secret'
access_token = 'replace your own account access_token'
access_secret = 'replace your own account access_secret'

auth = OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_secret)

#api = tweepy.API(auth)
api = tweepy.API(auth,proxy="127.0.0.1:1080")#代理

for status in tweepy.Cursor(api.home_timeline).items(2):#抓取条数
print (status.text)

运行代码获得对应twitter账号的首页推文