【问题标题】:Twitter User Profile can be extracted by thisTwitter用户配置文件可以通过这个提取
【发布时间】:2014-08-01 12:57:35
【问题描述】:

我能够使用 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

此代码可供任何人使用,任何人都可以将其与他/她自己的凭据一起用于单个用户配置文件。

【问题讨论】:

    标签: python tweepy


    【解决方案1】:

    终于锻炼和阅读了很多我得到了我的问题的答案。你可以试试这个

    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。

    【讨论】:

    • 我正在尝试以类似的方式获取用户地理位置。您能否分享一些关于这部分的见解
    • 你好,Sitz,请说明你想要什么。地理位置可以通过python的geopy模块提取。 Google 还提供其 API,用于直接获取任何特定位置的地理编码。
    【解决方案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 的tweepy 文档,第113 行)。

    【讨论】:

    • 感谢您的回复 Luigi。我试过了,但我无法生成用户对象
    • 我已经解决了获取用户列表详细信息的问题,您可以在 answers@Luigi 中查看上面的代码
    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多