分页游标时处理tweepy连接问题

2024-03-28 12:01:50 发布

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

我在用tweepy从twitter上获取一些对话。Tweepy有一个坏习惯,就是在出现连接问题的时候打破这个习惯,所以我正在尝试处理这个问题,如果出现连接问题,我会不断地重试,但是我很难弄清楚如何在通过游标分页时做到这一点。你知道吗

这是我的密码:

chains = []
for status in tweepy.Cursor(api.search, q='the&filter:replies', count=100).items():
    try:
        status_chain = get_status_chain(api, status.id_str)
        if len(status_chain) > 9:
            chains.append(status_chain)
    except Exception as e:
        # We hit a private message or a connection issue, skip to the next.
        print('Error: {error}. Retrying.'.format(error=e), file=sys.stderr)
        continue
    print(len(chains))
    if len(chains) > 99:
        break

这里有两个问题。最大的问题是try/except只包含在get_status_chain()中进行的调用。如果for status in tweepy.Cursor(api.search, q='the&filter:replies', count=100).items()行出错,异常将不被处理,我的程序将停止。如果这里发生错误,我希望在同一点继续重试,即不再启动整个for循环。你知道吗

第二个问题是,如果我们遇到连接问题,我真的很想再次尝试获取相同的链,而不是只去下一个,但我不知道如何干净地做到这一点。你知道吗


Tags: theinapichainforsearchlenstatus