微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重复请求时出现 TwitterConnectionError

如何解决重复请求时出现 TwitterConnectionError

以下代码使用 Twitter API,在获取“next_token”的同时重复获取推文。这会导致错误,但我不知道错误表示什么。我想知道我应该如何修改代码

这是我的代码

from TwitteraPI import TwitteraPI,TwitterPager
import csv
import pandas as pd
import time

#create a dataframe to store tweets
df = pd.DataFrame()

api = TwitteraPI(consumer_key,consumer_secret,auth_type='oAuth2',api_version='2')

#the first query without next_token
q = {'query':'(#abc -RT lang:en)','start_time': "2017-10-16T00:00:00Z",'end_time': "2018-03-31T23:59:59Z",'tweet.fields':'created_at','max_results': '500'}

r = api.request('tweets/search/all',q)
response_data = r.json()
df_a = pd.DataFrame(response_data['data'])
#Add tweets to dataframe
df = pd.concat([df,df_a])

#Loop this process while there's 'next_token'
while  'next_token' in response_data["Meta"]:
    #sleep not to exceed rate limit
    time.sleep(4)
    token = response_data["Meta"]['next_token']
    #query with 'next_token'
    q = {'query':'(#abc -RT lang:en)','max_results': '500','next_token': token}

    r = api.request('tweets/search/all',q)
    if r.status_code == 200:
        response_data = r.json()
        df_a = pd.DataFrame(response_data['data'])
        df = pd.concat([df,df_a])
    else:
        print("ERROR: %d" % res.status_code)

我收到如下错误

TwitterConnectionError: HTTPSConnectionPool(host='api.twitter.com',port=443): Read timed out. (read timeout=5)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。