使用tweepy爬twitter图片

来源:互联网 发布:淘宝卖家刷一单10元 编辑:程序博客网 时间:2024/06/05 08:19

1,首先请确保网络可以翻墙,系统使用linux ubuntu,系统自带python,可以下载一个idle方便操作:sudo apt-get install idle.
问题:使用lantern翻墙,只能用浏览器访问外网,终端上连不上。
测试:终端输入:ping www.twitter.com出现界面与ping www.baidu.com相同
2,访问http://www.apps.twitter
注册twitter账户->返回apps.twitter.com,登录twitter账户,create new app,根据提示填写信息。其中website若没有,填写默认值:http://placeholder.place,
3,创建成功后,获取到Consumer Key (API Key)和Consumer Secret (API Secret)这里写图片描述
下拉,生成Access Token和Access Token Secret
这里写图片描述
先不要关闭网页,这四个数据下面要用到。
4,终端下安装pip,可直接google方法,
5,终端下安装tweepy,twitter-photos。
先安装pip,然后用,pip install tweepy
pip install twitter-photos

5,代码:
import tweepy
import random
import io
import subprocess
consumer_key = “**
consumer_secret = “**
access_token = “**
access_token_secret = “**
**替换成上面得到的数据
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)
count=0
while(count < 1000):
x=random.randint(0,10000000)
#print x
try:
u = api.get_user(x)
if u.screen_name != str(x):
print u.screen_name
shell_cmd = “twphotos -u ” + u.screen_name
subprocess.call(shell_cmd, shell=True)
count+=1
except tweepy.TweepError,e:
print ‘error’
print ‘finish’
`

referrence: http://www.itdadao.com/articles/c15a15532p0.html
https://github.com/shichao-an/twitter-photos

原创粉丝点击