Flask + Scrapy:无法加载资源:net :: ERR_CONNECTION_RESET

时间:2020-05-20 14:24:31

标签: javascript python flask scrapy

我正在尝试在flask应用程序中运行脚本。有时我会收到此错误:

Failed to load resource: net::ERR_CONNECTION_RESET

我有以下python脚本(烧瓶应用程序)

@app.route('/run', methods=['POST'])
def run():
category = request.form.get('category')
settings = ''
with open('settings.json', 'r') as f:
    for line in f.read():
        settings += line

settings = json.loads(settings)
settings['category'] = category

with open('settings.json', 'w') as f:
    f.write(json.dumps(settings, indent=4))

process = subprocess.Popen('python3 scraper.py', shell=True)
process.wait()

output = ''  
with open('wellness.jsonl', 'r') as f:
    for line in f.read():
        output += line

output = [json.loads(item + '\n}') for item in output.split('}\n')[0:-1]]

return {'data': output}

html(输出布局):

 <div class="input-group">
    <input type="text" id="category" class="form-control col-3 mr-2">
    <button id="scrape" class="btn btn-success">Scrape</button>
  </div>
  
  <div id="output" class="mt-4"></div>
  
  <script type="text/javascript">
    $('#scrape').on('click', () => {
      var category = $('#category').val()
      $.post('/run', {'category': category}, (res) => {     
        $.each(res.data , function(index, card) { 
          var content = '<div class="card mb-3" style="max-width: 800px;">'
          content += '<div class="card-body">'
          content += '<h5 class="card-title"><strong>' + 
                     card.First_and_Last_Name + '</strong></h5>'
          content += '<p class="card-text">' + card.About + '</p>'
          content += '</div></div></div></div>'
          $('#output').append(content)
        });  
      })
    })
  </script>

我在这里做错了什么。任何和所有帮助表示赞赏。谢谢!

0 个答案:

没有答案