【发布时间】:2016-07-17 23:02:30
【问题描述】:
我制作了一个 Flask-Python 网络应用程序,它可以抓取皇马的赛程并将其显示在一个整洁的倒计时页面中。我正在尝试通过 Heroku 托管它。我将抓取部分移动到主 python 脚本并通过 render_template 传递抓取的变量。
我的问题是 Python 脚本如何在 Heroku 服务器上运行?当有人打开网页时调用它还是只运行一次并提供请求?如果是这样,是否有办法重新启动服务器或定期重新运行 Python 脚本,以便夹具的更改反映在网页中。
这是我的 app.py
import requests
import datetime
from bs4 import BeautifulSoup as bs
from lxml import html
url = 'http://www.realmadrid.com/en/football/schedule'
response = requests.get(url)
html = response.content
soup = bs(html)
loc = soup.find('p', {'class': 'm_highlighted_next_game_location'}).contents
loc1 = loc[0]
if "Santiago" in loc1:
opp = soup.find('div',{'class':'m_highlighted_next_game_team m_highlighted_next_game_second_team'}).strong.contents
else:
opp = soup.find('div', {'class': 'm_highlighted_next_game_team'}).strong.contents
opp1=opp[0]
time = soup.find('div', {'class': 'm_highlighted_next_game_info_wrapper'}).time.contents
time1 = time[0]
date = soup.find('header', {'class': 'm_highlighted_next_game_header'}).time.contents
date1 = date[0]
times = time1.split(":")
dates = date1.split("/")
hour = times[0]
mintemp = times[1]
minutes = mintemp[:-2]
year = dates[0]
month = dates[1]
day = dates[2]
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html',hour=hour,minutes=minutes,year=year,month=month,day=day,loc=loc1,opp=opp1)
if __name__ == '__main__':
app.run(debug=True)
P.S:我是第一次使用 Heroku。如果有些事情听起来很愚蠢,请原谅。
【问题讨论】: