我正在开展网络抓取项目,并希望从公共Twitter帐户获取关注者数据。我通常会找到请求网址,但我似乎无法在Twitter上找到它。所以我想知道如何获得粉丝列表。我正在使用Python 3
答案 0 :(得分:1)
This page描述了如何做到这一点。
您需要安装tweepy并使用以下代码:
import tweepy
import time
#insert your Twitter keys here
consumer_key ='xxxxxxx'
consumer_secret='xxxxxxx'
access_token='xxxxxxx'
access_token_secret='xxxxxxx'
twitter_handle='handle'
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
list= open('twitter_followers.txt','w')
if(api.verify_credentials):
print ('We successfully logged in')
user = tweepy.Cursor(api.followers, screen_name=twitter_handle).items()
while True:
try:
u = next(user)
list.write(u.screen_name +' \n')
except:
time.sleep(15*60)
print ('We got a timeout ... Sleeping for 15 minutes')
u = next(user)
list.write(u.screen_name +' \n')
list.close()