Create a web page in Flask. Click the button to execute an external scraping file.
Flask installation
pip install Flask
Create the original file
root.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello!'
if __name__ == "__main__":
app.run(debug=True)
Run
python root.py
afterwards
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Is output, so access http://127.0.0.1:5000/.
Hello! Is displayed.
Add import etc.
root.py
# from flask import Flask
#Add ↓
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello():
#return 'Hello!'
#Add ↓
return render_template('layout.html', title='Scraping App')
if __name__ == "__main__":
app.run(debug=True)
Create a templates folder and create layout.html in it. Make sure to send the GET method by clicking the button tag.
layout.html
<!doctype html>
<html>
<head>
<!-- ↓ render_Contains the title written in the template-->
<title>{{ title }}</title>
</head>
<body>
<div class="member">
<img src="/static/img/akimoto.jpg " alt="img1">
<h2>Midsummer Akimoto</h2>
<form method="GET" action="/scraping">
<button type="submit">Start Scraping</button>
</form>
</div>
</body>
</html>
Create a static folder, create css and img inside, and arrange the appearance.
root.py
from flask import Flask, render_template
#↓ Import the file you want to execute through Flask
import scraping
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('layout.html', title='Scraping App')
# ↓ /Processing when scraping is received by GET method
@app.route('/scraping')
def get():
#↓ Function of the file you want to execute
return scraping.scraping()
if __name__ == "__main__":
app.run(debug=True)
layout.html
<!doctype html>
<html>
<head>
<title>{{ title }}</title>
<link rel="stylesheet" href="/static/css/index.css">
</head>
<body>
<div class="member">
<img src="/static/img/akimoto.jpg " alt="img1">
<h2>Midsummer Akimoto</h2>
<form method="GET" action="/scraping">
<button type="submit">Start Scraping</button>
</form>
</div>
</body>
</html>
↓ The scraping file executed this time Get images of Nogizaka46 blog by scraping
You can now run Python files from the console through a web page!
Recommended Posts