I am creating a Flask app that I want to use to display some scraped information. I have imported my scraping file into Flask, and for its scraping function, I have to pass a name to search as a parameter. I want to get this name from an input box in my HTML code. I am trying to accomplish this using an Ajax call in jQuery, but I am currently getting a 404 error.
I have tried outputting the raw json data, versus a python list. I have tried using two separate Ajax calls. I have also tried looking at another post that I was pointed to on here, but I have run into this said 404 error.
Flask app code
#Start the flask app
app = Flask(__name__)
#Start page
@app.route('/')
def index():
return render_template('index.html')
#What happens when our button is clicked
@app.route('/_get_data')
def _get_data():
#Get the name we want to search for
searchName = request.args.get('searchName')
#Call the function and pass our search name parameter
dataList = scrape_data(searchName)
#Return the json format of the data we scraped
return jsonify(dataList = dataList)
#Run the app
if __name__ == "__main__":
app.run(debug = True)
index.html code
<!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>
<script type = text/javascript src = "{{url_for('static', filename = 'jquery.js') }}"></script>
<form id = "nameForm" role = "form">
<input name = "text">
<button id = "searchBtn"> Search </button>
</form>
<div id = "container"></div>
<script type = text/javascript> $SCRIPT_ROOT = {{ request.script_root|tojson|safe }}; //Get the root of our data </script>
<script type = "text/javascript">
//Root so we can get the data from our form
$('button#searchBtn').click(function(e) {
//Prevent our form from submitting
e.preventDefault();
//Get the root
$.getJSON($SCRIPT_ROOT + '/_get_data', {
//Our searchName variable will be the one from our HTML input box
searchName: $('input[name = "text"]').val(),
}, function(data) {
console.log(data.dataList);
});
});
</script>
</body>
</html>
Again, to be clear, my goal is to take the data from my HTML input form once by searchBtn
is clicked, and use this data as a string in my parameter to webscrape data. Then, once the scraped data is return, I am trying to log it to my console.py