问题描述
以下代码使用 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 (将#修改为@)