gpt4 book ai didi

python - APScheduler 和传递参数

转载 作者:行者123 更新时间:2023-12-01 08:36:04 26 4
gpt4 key购买 nike

目前,我正在使用 APScheduler 每 10 分钟自动执行一次脚本。它在 print("[+] Success! Bot Starting!") 之后停止执行,它不会输出错误。我建议我的 Scheduler.add_job(trendingBot, 'interval', 分钟=10, args=[url, browser]) 声明不正确,我不知道如何修复它。

# grabs all the trending quotes for that day
def getTrendingQuotes(browser):
# wait until trending links appear, not really needed only for example
all_trendingQuotes = WebDriverWait(browser, 10).until(
lambda d: d.find_elements_by_css_selector('#trendingQuotes a')
)
return [link.get_attribute('href') for link in all_trendingQuotes]


def getStockDetails(url, browser):

print(url)
browser.get(url)

quote_wrapper = browser.find_element_by_css_selector('div.quote-wrapper')
quote_name = quote_wrapper.find_element_by_class_name(
"quote-name").find_element_by_tag_name('h2').text
quote_price = quote_wrapper.find_element_by_class_name("quote-price").text
quote_volume = quote_wrapper.find_element_by_class_name(
"quote-volume").text

print("\n")
print("Quote Name: " + quote_name)
print("Quote Price: " + quote_price)
print("Quote Volume: " + quote_volume)
print("\n")

convertToJson(quote_name, quote_price, quote_volume, url)


quotesArr = []

# Convert to a JSON file


def convertToJson(quote_name, quote_price, quote_volume, url):
quoteObject = {
"url": url,
"Name": quote_name,
"Price": quote_price,
"Volume": quote_volume
}
quotesArr.append(quoteObject)


def trendingBot(url, browser):
browser.get(url)
trending = getTrendingQuotes(browser)
for trend in trending:
getStockDetails(trend, browser)
# requests finished, write json to file
with open('trendingQuoteData.json', 'w') as outfile:
json.dump(quotesArr, outfile)


def Main():
scheduler = BlockingScheduler()
chrome_options = Options()
chrome_options.add_argument("--headless")
# applicable to windows os only
chrome_options.add_argument('--disable-gpu')

url = 'https://www.tmxmoney.com/en/index.html'
browser = webdriver.Chrome(
r"C:\Users\austi\OneDrive\Desktop\chromeDriver\chromedriver_win32\chromedriver.exe", chrome_options=chrome_options)
browser.get(url)

os.system('cls')
print("[+] Success! Bot Starting!")
scheduler.add_job(trendingBot, 'interval', minutes=1, args=[url, browser])
scheduler.start()
#trendingBot(url, browser)
browser.quit()


if __name__ == "__main__":
Main()

最佳答案

APScheduler 默认情况下是非阻塞的,这意味着当您执行 scheduler.add_job(.. ) 和 scheduler.start() 时,您的应用程序将继续运行到 browser.quit() 并最终到达应用程序的末尾(退出的地方)。

您似乎想要一个阻塞调度程序,例如 BlockingScheduler .

更改行:

scheduler = BackgroundScheduler()

scheduler = BlockingScheduler()

关于python - APScheduler 和传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53714155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com