我能够使用Tweepy API提取有关Twitter用户的上述详细信息。 我想为用户列表做这件事。任何人都可以帮我这个吗?
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
twitterStream = Stream(auth,TweetListener())
user = api.get_user('User Name')
print user.screen_name
print user.description
print user.followers_count
print user.statuses_count
print user.url
此代码已准备就绪,任何人都可以将自己的凭据用于单个用户个人资料。
答案 0 :(得分:8)
最后锻炼和阅读很多我得到了我的问题的答案。你可以试试这个
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
#search
api = tweepy.API(auth)
twitterStream = Stream(auth,TweetListener())
test = api.lookup_users(user_ids=['17006157','59145948','157009365'])
for user in test:
print user.screen_name
print user.name
print user.description
print user.followers_count
print user.statuses_count
print user.url
此代码已准备好使用,只需将有效密钥放在ABC&获取用户个人资料。您需要先获取ID。
答案 1 :(得分:2)
您的代码只是与您的 Twitter帐户进行互动;要查找有关特定用户或用户组的信息,您应使用api.lookup_users(user_ids=[])
查询查找这些信息。
你这样做:
#boring auth you already have
import tweepy
from tweepy import OAuthHandler
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
#search
api = tweepy.API(auth)
test = api.lookup_users(user_ids=['1123728482,5539932'])
这为您提供了两个tweepy.models.User
对象的列表:
[<tweepy.models.User object at 0x103995090>, <tweepy.models.User object at 0x1039950d0>]
您可以将user_ids
中的列表替换为最多100个ID的列表,但不会让Twitter一次搜索到这个列表。获得User
个对象列表后,您可以访问不同的属性(有关列表,请查看User class第113行的tweepy文档。)