【发布时间】:2019-12-12 01:24:11
【问题描述】:
我正在努力制作一个我想要制作的应用程序,这是一个 Web 应用程序,它将使用带有 chromedriver 的 selenium 包抓取其他网站。 我在基本操作系统上使用 python 3.7。 如果我不包括加载页面,一切正常,即使我还没有把它放到网上所以我不确定,但我的问题是我想让爬行无头和没有沙箱,同时等待我的应用程序中的页面。 我也想在工作完成后渲染成一个成功的 html 模板。
我在 stackoverflow 上搜索了答案,但我无法让我的东西正常工作。 (对不起,如果重复) 我发现的一件事是 Luiz Aoqui 对这个问题的回答:Flask is not render_template before executing long function,看来 OP 可以解决他的问题。 不过我做不到。 我根本不懂javascript,所以如果问题来自这里,也许你会发现这很简单。
python烧瓶代码:
@app.route('/auto_connect/', methods=["GET", "POST"])
def connect():
if session['mail'] != None:
if request.method == "POST":
session['job'] = request.form['job']
return redirect(url_for('process', fun='auto_connect'))
return render_template('auto_connect.html')
return redirect(url_for('login'))
@app.route("/process/<fun>", methods=['GET', 'POST'])
def process(fun, *args):
if fun == 'auto_connect' or fun == 'auto_apply':
if request.method == 'GET':
return render_template('wait.html', fun=fun)
if request.method == 'POST':
print('test')
if fun == 'auto_connect':
auto_connector(session['mail'], session['password'], session['job'])
return 'done'
elif fun == 'auto_apply':
auto_applyer(session['mail'], session['password'], session['job'], session['location'])
return 'done'
else:
return "error"
return 'error'
JS代码:
var request = new XMLHttpRequest();
request.open('POST', '/process/'.concat({{fun}}));
request.onload = function() {
if (request.status === 200 && request.responseText === 'done') {
// long process finished successfully, redirect user
window.location = '/success/' ;
} else {
// ops, we got an error from the server
alert('Something went wrong. FROM server');
}
};
request.onerror = function() {
// ops, we got an error trying to talk to the server
alert('Something went wrong. TO server');
};
request.send();
正在显示加载页面,但没有开始抓取。 我希望它以 js 代码中的“打开”POST 请求开始,该请求是“wait.html”模板的一部分。
PS:有烧瓶调试器,我在我的抓取脚本顶部放了一个打印,当我不渲染加载页面时,它会显示在终端中,但当我这样做时不会。
127.0.0.1 - - [04/Aug/2019 02:03:20] "GET /auto_connect/ HTTP/1.1" 200 -
127.0.0.1 - - [04/Aug/2019 02:03:22] "POST /auto_connect/ HTTP/1.1" 302 -
127.0.0.1 - - [04/Aug/2019 02:03:22] "GET /process/auto_connect HTTP/1.1" 200 -
提前感谢您的回答。
【问题讨论】:
标签: javascript python ajax selenium flask