【问题标题】:Loading page in flask web app while scraping another website using selenium在使用 selenium 抓取另一个网站的同时在烧瓶 Web 应用程序中加载页面
【发布时间】: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


    【解决方案1】:

    我自己找到了问题的答案。 我不是网络程序员,所以有时我缺乏一些自动化,但是通过检查我的等待页面,我在 concat 函数的 JS 脚本中看到了一个错误。 在脚本中调用 '{{fun}}' 而不是 {{fun}} 就可以了。

    我不理解这种行为,因为我的对象 fun 是一个 python 字符串,但是我阅读了 jinja2 文档并且对我来说一切都变得清晰了,我们可以在这个链接https://jinja.palletsprojects.com/en/2.10.x/templates/ 中看到以下内容:

    {{ ... }} for Expressions to print to the template output
    
    

    这会打印值,因此它不再是字符串,而是 html 模板中的原始文本。 (type(print(x)) 在 python3 中是 None 顺便说一句)

    如果这对某人有帮助,那我很高兴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 2021-03-18
      相关资源
      最近更新 更多