简体   繁体   English

如何实现“如果错误等待 15 分钟然后继续”?

[英]how to implement an "If error wait for 15min then continue"?

I am trying to scrape data(liking user of tweets) from twitter and save to a sqlite3 database.我正在尝试从 twitter 抓取数据(喜欢推文的用户)并保存到 sqlite3 数据库。 The twitter API allows us to do 75 requests at once then raises a "too many requests" error. twitter API 允许我们一次执行 75 个请求,然后引发“请求过多”错误。 I want to implement a mechanism to: if there is an error we will wait for 15min then continue.我想实现一种机制:如果出现错误,我们将等待 15 分钟然后继续。 The program should not start over but continue to send the requests where the error was raised and 15min of sleep time.程序不应重新开始,而是继续发送引发错误的请求和 15 分钟的睡眠时间。 Any ideas please:!任何想法请:! for the getClient() one should have his own credintials my code is:对于 getClient() 应该有他自己的凭据,我的代码是:

import pandas as pd
import json

def getClient():
    client = tweepy.Client(bearer_token=BEARER_TOKEN,
                           consumer_key=API_KEY,
                           consumer_secret=API_KEY_SECRET,
                           access_token=ACCESS_TOKEN,
                           access_token_secret=ACCESS_TOKEN_SECRET)
    return client
def intersection(lst1, lst2):
    lst3 = [value for value in lst1 if value not in lst2]
    return lst3

def addLikers(client):
    conn = sqlite3.connect("data.db")
    ids = pd.read_sql_query("SELECT tweetId FROM searchTweets", conn)['tweetId'].tolist()
    likes = pd.read_sql_query("SELECT tweetId FROM LikingUsers", conn)['tweetId'].tolist()
    communVals = intersection(ids, likes)
    c = conn.cursor()
    for communVal in communVals:
# the function should be here
        likingUsers = client.get_liking_users(communVal)
        row = [(communVal), (json.dumps(likingUsers.data))]
        c.executemany("INSERT INTO searchFollowers VALUES (?,?)", (row,))
        conn.commit()

addLikers(client)

For now I am just committing the results until i get the error.现在我只是提交结果,直到我得到错误。 To create the table in sqlite3:在 sqlite3 中创建表:

import sqlite3

conn = sqlite3.connect("data.db")

c = conn.cursor()
c.execute("""CREATE TABLE LikingUsers (
        tweetId integer,
        reply text
        )""")

you can use signal :你可以使用signal


    def signal_handler(signum, frame):
        raise Exception("Timed out!")

  # limit the time before specific function
            signal.signal(signal.SIGALRM, signal_handler)
            signal.alarm(60 * 15)  # 15 minuets

暂无
暂无

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

相关问题 如何在不使用最小/最大/总和或平均值的情况下将 dataframe 的日期时间值分配给下一个 15 分钟时间步长? - How to asign Datetime values of a dataframe to the next 15min Timestep without using min/max/sum or mean? 如何使用时间间隔设置xaxis,例如使用matplotlib设置15分钟 - how to set the xaxis in time interval, say 15min using matplotlib 推断数据帧以计算 15 分钟和 30 分钟的平均值 - Extrapolating dataframes to calculate 15min and 30min averages NumPy数组15分钟值-每小时平均值 - Numpy array 15min values - hourly mean values Python Pandas 对数据点之间的平均值进行上采样(15 分钟到 1 分钟) - Python Pandas Upsampling on average values between data points (15min to 1min) 将时间戳汇总到15分钟(以小时为单位),并找到熊猫中多列的总和,平均和最大值 - Aggregate to 15min based timestamp to hour and find sum, avg and max for multiple columns in pandas 需要帮助让 pandas 识别我的时间序列 dataframe 间隔为 15 分钟 - Need help getting pandas to recognize my timeseries dataframe is in 15min intervals Python:将时间戳四舍五入到 15 分钟间隔,附加到 df 但缺少小时、分钟和秒,dtype:datetime64[ns] - Python: Rounding timstamp to 15min interval, appended to a df but missing hours, minutes and seconds, dtype: datetime64[ns] 错误舍入到前15分钟的时间-Python - Error rounding time to previous 15 min - Python 如何计算每分钟数据集的 15 分钟标准偏差? - How to compute 15-min standard deviations of a minutely dataset?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM