Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从第二次开始,用户在使用Flask构建的网站上提交表单后,无法成功执行python web scraping脚本_Python_Flask_Screen Scraping_Mechanize_Cookiejar - Fatal编程技术网

从第二次开始,用户在使用Flask构建的网站上提交表单后,无法成功执行python web scraping脚本

从第二次开始,用户在使用Flask构建的网站上提交表单后,无法成功执行python web scraping脚本,python,flask,screen-scraping,mechanize,cookiejar,Python,Flask,Screen Scraping,Mechanize,Cookiejar,使用Flask和Python,我在localhost上运行了一个网站,允许用户选择一个特定的月份来下载报告。根据所选月份,我将导入我的网页抓取文件,从另一个网站检索数据(需要登录)。我的网页抓取脚本使用Mechanize 下面是在单击下载按钮(选择在office.html上完成)后导入web抓取文件(webscrape.py)的代码部分: 在render_template方法中,success=True作为参数传递,以便my office.html脚本将显示一条成功消息,如果不是(当它是GET请

使用Flask和Python,我在localhost上运行了一个网站,允许用户选择一个特定的月份来下载报告。根据所选月份,我将导入我的网页抓取文件,从另一个网站检索数据(需要登录)。我的网页抓取脚本使用Mechanize

下面是在单击下载按钮(选择在office.html上完成)后导入web抓取文件(webscrape.py)的代码部分:

在render_template方法中,success=True作为参数传递,以便my office.html脚本将显示一条成功消息,如果不是(当它是GET请求时),它将显示供用户选择的表单。以下是我的office.html脚本:

@app.route('/office/', methods=['GET','POST'])
def office():
    form=reportDownload()
    if request.method=='POST':
        import webscrape
        return render_template('office.html', success=True)
    elif request.method=='GET':
        return render_template('office.html', form=form)
{% extends "layout.html" %}
{% block content %}
  <h2>Office</h2>
  {% if success %}
    <p>Report was downloaded successfully!</p>
  {% else %}
    <form action="{{ url_for('office') }}" method="POST">
      <table width="70%" align="center" cellpadding="20">
      <tr>
        <td align="right"><p>Download report for: </p></td>
        <td align="center"><p>Location</p>
                  {{form.location}}</td>
        <td align="center"><p>Month</p> 
                             {{form.month}}  </td>
        <td align="center"><p>Year</p>   
                             {{form.year}}  </td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td align="center">{{form.submit}} </td>
      </tr>
    </table>
   </form>
   {% endif %}
{% endblock %}
然后我继续抓网

在我的终端(或命令提示符)上运行web scraping文件时,即使我第二次或第三次运行脚本,脚本也不会出现任何问题。所以我认为这可能是网站代码的问题

如有任何建议,将不胜感激!我尝试了不同的方法来解决这个问题,比如使用返回重定向,或者尝试清除cookiejar中的cookies。到目前为止,还没有一个有效的方法,或者我可能错误地使用了这些方法


提前谢谢你

启动Flask应用程序后,每个软件包只导入一次。这意味着,当它第二次运行到
import webscrape
时,它会说“好吧,我之前已经导入了,所以不需要采取进一步的操作…”并继续下一行,在不实际启动脚本的情况下呈现模板

从这个意义上讲,Python中的
import
与其他语言中的
require
不同(比如PHP;顺便说一下,它更接近PHP中的
require\u once


解决方案是将您的scraper设置为对象(
class
),并在每次需要时实例化它。然后将导入移动到文件顶部,并在
if request.method=='POST'
中创建一个新的web scraper实例。

非常感谢@cuducos,它现在可以工作了!:)感谢您如此清晰地解释问题和解决方案!
  br = mechanize.Browser()
  cj = cookielib.LWPCookieJar()
  br.set_cookiejar(cj)