[英]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.