【发布时间】:2019-11-01 15:18:09
【问题描述】:
我正在创建一个烧瓶网络应用程序,我想在一个外部文件上显示一些我用 BS4 抓取的信息。我已经导入了该文件的函数并调用了数据,我的问题是我的错误是什么?我希望能够使用 Flask 发布这些数据,然后使用 Ajax 调用来调用数据,以便我可以操作我的 HTML 页面。
我尝试了很多不同的方法,包括使用methods = ['GET', 'POST],以及更改我的_get_data(): 函数
Flask 应用文件
#Import dependencies
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import pandas as pd
import requests
import json
from scrape import *
from flask import Flask, render_template, jsonify, request, escape, url_for
app = Flask(__name__)
#Get out list to post
data = scrape_data()
headers = data_headers()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/_get_data', methods = ['GET','POST'])
def _get_data():
data = request.form.getlist('data[]')
print(data)
return render_template('index.html')
if __name__ == "__main__":
app.run(debug = True)
HTML 文件
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>NBA Data Web App</title>
</head>
<body>
<script src = "http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" crossorigin = "anonymous"></script>
<button id = "searchBtn"> Search </button>
<div id = "response"></div>
<script type = "text/javascript">
var headers =
$('button#searchBtn').click(function() {
$.ajax({
type: "POST",
url: "/_get_data",
data : {'data': dataList},
success: function(resp) {
$('div#response').append(resp.data)
}
});
});
console.log(dataList);
</script>
<ul>
{% for elem in dataList %}
<li>{{elem}}</li>
{% endfor %}
</ul>
</body>
</html>
编辑:我目前正在使用的代码
【问题讨论】:
标签: ajax python-3.x post flask get