Tweepy:Twitter错误响应:状态码= 500

时间:2018-07-20 09:48:44

标签: python twitter tweepy hashtag

我目前正在尝试使用扭曲光标获取给定标签的所有推文。为此,我创建了下面的函数,该函数将特定的标签作为参数,并将标签的时间戳和用户的关注者数量写入csv文件中。 对于某些主题标签,这可以很好地工作,并且我得到了所需的结果(在7天的时间里168小时出现主题标签),例如为#promotion。对于其他一些程序,执行将在大约6小时后停止,并给出以下错误,例如#WorldEmojiDay或#marketing。我已经知道了错误的含义,而且我不知道如何处理它。任何帮助将不胜感激。

程序代码:

def hashtag_collect(hashtag_name):

    consumer_key='xxx'
    consumer_secret='xxx'
    access_token='xxx'
    access_token_secret='xxx'

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)

    # Open/Create a file to append data
    csvFile = open('data_collect.csv', 'a')
    time_followers = open(('collect_17-07/time_and_followers_%s.csv' % hashtag_name), 'a')
    #Use csv Writer
    csvWriter = csv.writer(csvFile)
    csvWriter_data= csv.writer(time_followers)
    csvWriter_data.writerow(['timestamps','followers'])

    print(hashtag_name)
    dates=['2018-07-12','2018-07-13','2018-07-14','2018-07-15','2018-07-16','2018-07-17','2018-07-18','2018-07-19']
    #print(hashtag_name)
    for i in range(0,len(dates),1):
        print(i,dates[i])
        for tweet in tweepy.Cursor(api.search,q=hashtag_name,count=200,since="2018-07-09", lang='en').items(): #nb: date au format Year-month-day
            #print(i, dates[i])
            csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8'),tweet.user.followers_count, time.mktime(time.strptime(str(tweet.created_at),"%Y-%m-%d %H:%M:%S"))]) #write date, tweet, nb of followers, timestamp
            csvWriter_data.writerow([time.mktime(time.strptime(str(tweet.created_at),"%Y-%m-%d %H:%M:%S")),tweet.user.followers_count])

返回错误:

Traceback (most recent call last):
  File "collect.py", line 59, in <module>
    hashtag_collect(tag)
  File "collect.py", line 33, in hashtag_collect
    for tweet in tweepy.Cursor(api.search,q=hashtag_name,count=200,since="2018-07-09", lang='en').items(): #nb: date au format Year-month-day
  File "/usr/local/lib/python3.6/dist-packages/tweepy/cursor.py", line 49, in __next__
    return self.next()
  File "/usr/local/lib/python3.6/dist-packages/tweepy/cursor.py", line 197, in next
    self.current_page = self.page_iterator.next()
  File "/usr/local/lib/python3.6/dist-packages/tweepy/cursor.py", line 108, in next
    data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
  File "/usr/local/lib/python3.6/dist-packages/tweepy/binder.py", line 250, in _call
    return method.execute()
  File "/usr/local/lib/python3.6/dist-packages/tweepy/binder.py", line 234, in execute
    raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: Twitter error response: status code = 500

1 个答案:

答案 0 :(得分:0)

这是内部服务问题 https://developer.twitter.com/en/docs/basics/response-codes.html

除了稍后重试外,您无能为力。