返回剩余的Tweepy点击数

Return the number of remaining hits tweepy(返回剩余的Tweepy点击数)
本文介绍了返回剩余的Tweepy点击数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我正在尝试以下代码,以便读取ID列表并获取它们的对应名称。我正在尝试使用reamin_search_limits,以避免Rate_Limit错误。

limits = api.rate_limit_status()
remain_search_limits = limits['resources']['search']['/search/tweets']['remaining']
stream = open('myfile','w')
ss     = open('userNames', 'w')
for ids in content:
try:
    limits = api.rate_limit_status()
    remain_search_limits = limits['resources']['search']['/search/tweets']['remaining']
    print 'you have', remain_search_limits, 'API calls remaining until next hour'
    if remain_search_limits < 2:
         dtcode = datetime.utcnow()
         unixtime = calendar.timegm(dtcode.utctimetuple())
         sleeptime = rate_limit_json.get('reset_time_in_seconds') - unixtime + 10
         print 'waiting ', sleeptime, 'seconds'
         sleep(sleeptime)
    else:
         user = api.get_user(ids) 
         stream.write(str(user.id)+"
")
         ss.write(str(user.name)+"
")
except (tweepy.TweepError) as e:
    print e 
    stream.close()
    ss.close()

每次打印RESTAIN_SEARCH_LIMITS时返回180,直到出现tweepError异常。

推荐答案

此示例显示如何访问剩余的tweet数量。

print rate_limit_json["resources"]["search"]['/search/tweets']['remaining']
180

"resources" is the key you should be using to access the information inside.

如果您要更新值,请将其放入循环中,并在计时后重新赋值。睡眠()。

将所有代码放在一个While循环中: 大概是这样的:

limits = api.rate_limit_status()
remain_search_limits = limits['resources']['search']['/search/tweets']['remaining']
while remain_search_limits >2:
  limits = api.rate_limit_status()
  remain_search_limits = limits['resources']['search']['/search/tweets']['remaining']
else:
   dtcode = datetime.utcnow()
   unixtime = calendar.timegm(dtcode.utctimetuple())
   sleeptime = rate_limit_json.get('reset_time_in_seconds') - unixtime + 10
   print 'waiting ', sleeptime, 'seconds'
   sleep(sleeptime)

我尚未测试代码,但它应该接近您需要的内容。

您可能希望在两次调用之间休眠,我不熟悉该API,因此不确定您到底在做什么。

这篇关于返回剩余的Tweepy点击数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)