python-3.x - Twitter Api 的时间延迟问题
问题描述
我有一个 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, {}
应用时间延迟并将结果存储为单独列的任何建议
解决方案
你可以试试这段代码,它会捕获异常并等待 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
推荐阅读
- javascript - Vue.js。打开固定侧边栏时如何移动主要内容?
- visual-studio-code - 如何开发支持汇编级调试的 vscode 扩展?
- c# - 如何使用来自 HTTPClient 的 CookieContainer 的 cookie 在 Selenium 驱动程序上设置 cookie?
- arrays - 如何修改 Janusgraph(Gremlin) 服务器 API 响应、字符串数组
- python - 如何将使用 np.linspace 创建的行向量更改为列向量?
- django - 在创建对象之前进行验证并通过一个序列化程序创建多个模型数据
- laravel - 如何以低优先级执行 laravel 作业?
- azure - Azure Kubernetes - 每个节点只执行一个部署?
- python - Tkinter:并排对齐两个框架
- ssl - 在 .Netframework 2.0 中的 Windows 可执行文件上启用 TLS 1.2