Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 8 |
+
def home():
|
| 9 |
+
return render_template('home.html')
|
| 10 |
+
|
| 11 |
+
@app.route('/scrap', methods=['GET', 'POST'])
|
| 12 |
+
def scrap():
|
| 13 |
+
if request.method == 'POST':
|
| 14 |
+
url = request.form.get('url')
|
| 15 |
+
try:
|
| 16 |
+
response = requests.get(url)
|
| 17 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 18 |
+
headers = request.form.get('Headers')
|
| 19 |
+
|
| 20 |
+
# Extract titles or headers (h1, h2, h3)
|
| 21 |
+
titles = [title.get_text(strip=True) for title in soup.find_all(headers)]
|
| 22 |
+
|
| 23 |
+
return render_template('result.html', titles=titles)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"An error occurred: {e}"
|
| 26 |
+
else:
|
| 27 |
+
return redirect(url_for('home'))
|
| 28 |
+
|
| 29 |
+
if __name__ == '__main__':
|
| 30 |
+
app.run(debug=True)
|