如何改进我的代码以在tweepy中获得朋友

2024-09-26 17:51:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个代码来为twitter上的id列表挑选朋友。 但是API问题使得这段代码非常慢。 有可能改善吗? 我的代码是:

import tweepy
consumer_key = ''
consumer_key_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)

for cara in fin1:
    if cara in dici1.keys(): next
    else:
        amigos=[]
        for user in tweepy.Cursor(api.friends, screen_name=cara).items():
            time.sleep(60)
            try:
                amigos.append(user.screen_name)
                comum = [pessoa for pessoa in amigos if pessoa in fin1]
                dici = {cara : comum}
                dici1.update(dici)    
            except: time.sleep(60), next

fin1是ID列表(用户名,共39个)

dici1是dict,是我存储信息的地方


Tags: key代码intokenauthforsecretaccess
1条回答
网友
1楼 · 发布于 2024-09-26 17:51:55

删除time.sleep调用,这不是必需的,您还有一些根本没有意义的东西,比如那些next

import tweepy
consumer_key = ''
consumer_key_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)

for cara in fin1:
    if cara in dici1 
        continue

    amigos = []
    for user in tweepy.Cursor(api.friends, screen_name=cara).items():
        try:
            amigos.append(user.screen_name)
            comum = [pessoa for pessoa in amigos if pessoa in fin1]   
            dici1[cara] = comum
        except:
            time.sleep(60)

相关问题 更多 >

    热门问题