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

标签 python twitter tweepy hashtag

我目前正在尝试使用 tweepy 光标获取给定主题标签的所有推文。为了实现这一目标,我创建了下面的函数,该函数将特定的主题标签作为参数,并在 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

最佳答案

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

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

关于python - Tweepy:Twitter 错误响应:状态代码 = 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51439811/

相关文章:

python - Tweepy 光标搜索查询可以遍历从文件加载的单词列表吗?

python - 每次函数调用时导入 Redshift Python UDF

python - 使用 SWIG 将 numpy 数组元素(int)传递给 c++ int

python - 使用Python通过正则表达式访问Web数据不起作用

twitter - 去建立事件未定义

python-3.4 - 名称错误 - 名称 api 未定义

python - 哪个是支持代码重新加载的简约 python wsgi 开发服务器?

api - 在 Twitter Api 中通过 id 获取用户名

Twitter API请求限制问题

Python + Tweepy (STREAM API) - 解析某些值/对象的 JSON 输出