【发布时间】:2020-10-07 20:13:35
【问题描述】:
所以,基本上,我有一个无限循环,每隔几分钟就会不断地抓取一个网站。然而,为了启动这个过程,我只是导航到 /scraper 来启动新线程,虽然这工作得很好,但我不想继续路由到 /scraper 来启动线程,如果 Heroku 重新启动他们的服务器维护。有没有一种方法可以实现这一点,这样如果我路由到主页/我只能运行一个线程,因为我宁愿通过路由到主页而不是 /scraper 来启动爬虫。
def scrappy():
while True:
try:
print("Loading scraper...")
exec(open("texas_scraper.py").read()) #time.sleep(300) inside this .py
except Exception as e:
print("Error: There may be a bug in the code or your environment setup.")
print(e)
time.sleep(60)
continue
@app.route('/')
def index():
print("Someone is on homepage")
return '''
<html><body>
Hi. <a href="/uploads"> Please click here to download the roster.</a>
</body></html>
'''
@app.route('/scraper')
def digger():
t1 = Thread(target = scrappy)
t1.setDaemon(True)
t1.start()
print("Someone started the scraper")
return '''
<html><body>
Hi. Loading scraper now!
</body></html>
'''
【问题讨论】:
标签: python flask heroku flask-sqlalchemy