File size: 918 Bytes
dbef2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from flask import Flask, render_template, request, redirect, url_for
import requests
from bs4 import BeautifulSoup

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
    return render_template('home.html')

@app.route('/scrap', methods=['GET', 'POST'])
def scrap():
    if request.method == 'POST':
        url = request.form.get('url')
        try:
            response = requests.get(url)
            soup = BeautifulSoup(response.content, 'html.parser')
            headers = request.form.get('Headers')

            # Extract titles or headers (h1, h2, h3)
            titles = [title.get_text(strip=True) for title in soup.find_all(headers)]

            return render_template('result.html', titles=titles)
        except Exception as e:
            return f"An error occurred: {e}"
    else:
        return redirect(url_for('home'))

if __name__ == '__main__':
    app.run(debug=True)