返回剩下的点击数量

时间:2014-05-14 11:29:39

标签: python 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)+"\n")
         ss.write(str(user.name)+"\n")
except (tweepy.TweepError) as e:
    print e 
    stream.close()
    ss.close()

每次打印remain_search_limits,它返回180,直到得到tweepError异常。

1 个答案:

答案 0 :(得分:1)

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

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

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

如果要更新值,请将其置于循环中,在time.sleep()之后重新分配值。

将所有代码放在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不熟悉,所以不确定你到底在做什么。