简体   繁体   English

Twitter Api 的时间延迟问题

[英]Issue with the time delay for Twitter Api

I have a dataframe of twitter account, I am trying to call a photometer( https://github.com/IUNetSci/botometer-python ) to fetch the details, below is the sample data and code我有一个 twitter 帐户的 dataframe,我正在尝试调用光度计( 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

Issue: I am not to apply the pause after 15 minutes if the API calls more than 180 accounts and issue is that I am not able to store the result in a separate column.问题:如果 API 调用超过 180 个帐户并且问题是我无法将结果存储在单独的列中,我不会在 15 分钟后应用暂停。 The output from a photometer is a dictionary.光度计中的 output 是一本字典。

Expected output预期 output

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, {}

Any suggestion to apply the time delay and store the result as separate column应用时间延迟并将结果存储为单独列的任何建议

You can try this code, it will catch the exception and wait for 600 seconds, you can store the result from botometer as a list.你可以试试这个代码,它会捕获异常并等待 600 秒,你可以将来自 botometer 的结果存储为一个列表。 You can directly store the bot score with result['scores']['english']您可以使用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM