【问题标题】:Tweepy: get all friends of a sample of twitter accounts: how to handle protected usersTweepy:获取 Twitter 帐户样本的所有朋友:如何处理受保护的用户
【发布时间】:2017-08-24 02:16:18
【问题描述】:

我想查找一个 twitter 帐户的朋友样本中的所有朋友(即正在关注的 twitter 用户),看看他们还有哪些共同的朋友。问题是我不知道如何处理受保护的帐户,并且一直遇到此错误:

tweepy.error.TweepError: Not authorized.

这是我的代码:

...
screen_name = ----
file_name = "followers_data/follower_ids-" + screen_name + ".txt"
with open(file_name) as file:
ids = file.readlines()

num_samples = 30
ids = [x.strip() for x in ids]
friends = [[] for i in range(num_samples)]

for i in range(0, num_samples):
    id = random.choice(ids)
    for friend in tweepy.Cursor(api.friends_ids, id).items():
        print(friend)
        friends[i].append(friend)

我有一个来自一个帐户 screen_name 的所有朋友的列表,我从中加载了朋友 ID。然后,我想对其中一些进行抽样并查找他们的朋友。

我也试过这样的:

def limit_handled(cursor, name):
    try:
        yield cursor.next()
    except tweepy.TweepError:
        print("Something went wrong... ", name)
        pass

for i in range(0, num_samples):
    id = random.choice(ids)
    items = tweepy.Cursor(api.friends_ids, id).items()
    for friend in limit_handled(items, id):
        print(friend)
        friends[i].append(friend)

但是,在继续下一个示例之前,似乎每个示例朋友只存储了一个朋友。我对 Python 和 Tweepy 还很陌生,所以如果有什么奇怪的地方,请告诉我。

【问题讨论】:

  • 您的缩进似乎有点不对劲。请更正这一点。
  • 这个有几处要改的,等我有空再去看看,如果到时候没人回答的话。一个问题 - 您是否希望将无法查看的帐户作为示例之一包含在内,还是希望将其替换为您可以查看的帐户(如果可能)?
  • 不,我不需要我无法查看的帐户,用另一个帐户替换它们就足够了。谢谢

标签: python twitter tweepy


【解决方案1】:

首先,关于命名的几个cmets。名称 fileid 是受保护的,因此您应该避免使用它们来命名变量 - 我已经更改了这些。

其次,当你初始化你的 tweepy API 时,如果你使用wait_on_rate_limit=True,它足够聪明地处理速率限制,如果你使用wait_on_rate_limit_notify=True,它会在由于速率限制而延迟时通知你。

当您设置friends = [[] for i in range(num_samples)] 时,您也会丢失一些信息,因为您将无法将您找到的朋友与他们相关的帐户相关联。您可以改为使用字典,它将使用的每个 ID 与找到的朋友相关联,以便更好地处理。

我更正的代码如下:

import tweepy
import random

consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_token_secret = '...'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Creation of the actual interface, using authentication. Use rate limits.
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

screen_name = '----'
file_name = "followers_data/follower_ids-" + screen_name + ".txt"
with open(file_name) as f:
    ids = [x.strip() for x in f.readlines()]

num_samples = 30
friends = dict()

# Initialise i
i = 0

# We want to check that i is less than our number of samples, but we also need to make
# sure there are IDs left to choose from.
while i <= num_samples and ids:
    current_id = random.choice(ids)

    # remove the ID we're testing from the list, so we don't pick it again.
    ids.remove(current_id)

    try:
        # try to get friends, and add them to our dictionary value if we can
        # use .get() to cope with the first loop.
        for page in tweepy.Cursor(api.friends_ids, current_id).pages():
            friends[current_id] = friends.get(current_id, []) + page
        i += 1
    except tweepy.TweepError:
        # we get a tweep error when we can't view a user - skip them and move onto the next.
        # don't increment i as we want to replace this user with someone else.
        print 'Could not view user {}, skipping...'.format(current_id)

输出是一个字典,friends,其中包含每个用户的用户 ID 和好友项的键。

【讨论】:

    猜你喜欢
    • 2015-01-03
    • 2014-12-05
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多