有关Twitter Api的时间延迟问题

问题描述 投票:0回答:1

我有一个twitter帐户的数据框,我试图调用一个光度计(https://github.com/IUNetSci/botometer-python)来获取详细信息,下面是示例数据和代码

authors,count
generate_output,6
dismisstrump,6
luciano_ocasio,6
Jenny72166737,5
Hong18249170,5
anas_erindra,5
JayChance12,5
viralvm69,5
89nuncamais,5
ngaruman,4
sixyelcastaneda,4
Debisriprasad,4

import pandas as pd
import botometer
import nltk
from datetime import datetime
import tweepy
import time

author_details=[]
for i, row in df.iterrows():
   try:
      print(i)
      start = datetime.now()
      a_name='@'+row['authors']
      result = bom.check_account(a_name)
      a_details=(a_name, result)
      a_details=dict([(a_name, result)]) 
      author_details.append(a_details)
      now = datetime.now()
      d_time = (now - start).total_seconds()
      if int(d_time) < 900 and i<180:
        pass
      else:
        time.sleep(100)
   except Exception as e:
      print(e)
      continue

问题:如果API调用了180个以上的帐户,则我不会在15分钟后应用暂停,原因是我无法将结果存储在单独的列中。光度计的输出是一个字典。

预期输出

authors,count, result
generate_output,6, {}
dismisstrump,6, {}
luciano_ocasio,6, {}
Jenny72166737,5, {}
Hong18249170,5, {}
anas_erindra,5, {}
JayChance12,5, {}
viralvm69,5, {}
89nuncamais,5, {}
ngaruman,4, {}
sixyelcastaneda,4, {}
Debisriprasad,4, {}

任何建议应用时间延迟并将结果存储在单独的列中

python-3.x pandas twitter
1个回答
1
投票

您可以尝试使用此代码,它将捕获异常并等待600秒,您可以将botometer的结果存储为列表。您可以直接将机器人得分与result ['scores'] ['english']

一起存储
err="429 Client Error: Too Many Requests for url: https://osome- 
botometer.p.rapidapi.com/2/check_account"
df['details']=''
df['bot_score']=''
for i, row in df.iterrows():
   try:
    start = datetime.now()
    a_name='@'+row['authors']
    result = bom.check_account(a_name)
    df.at[i,'det']=[result]
    df.at[i,'bot_score'] = int((result['scores']['english'])*100)
 except Exception as e:
    if e==err:
        time.sleep(600)
    continue
© www.soinside.com 2019 - 2024. All rights reserved.