You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kmftools/bookscraper/app.py

54 lines
1.2 KiB

from flask import Flask, request, render_template_string
from scraper.book_scraper import BookScraper
from scraper.sites import BookSite
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
app = Flask(__name__)
# --- GET: toon formulier ---
@app.route("/", methods=["GET"])
def index():
return render_template_string("""
<html>
<body>
<h2>BookScraper</h2>
<form method="post">
<label>Book URL:</label><br>
<input name="url" style="width:400px"><br>
<button type="submit">Scrape</button>
</form>
</body>
</html>
""")
# --- POST: scraper uitvoeren ---
@app.route("/", methods=["POST"])
def run_scraper():
url = request.form.get("url")
site = BookSite()
scraper = BookScraper(site, url)
result = scraper.execute()
return render_template_string("""
<html>
<body>
<h2>Scrape result: {{title}}</h2>
<h3>Debug output:</h3>
<pre style='background:#111;color:#0f0;padding:10px;border-radius:8px'>
{{debug}}
</pre>
<p><a href="/">Terug</a></p>
</body>
</html>
""", title=result["title"], debug=result["debug"])
if __name__ == "__main__":
app.run(debug=True)