-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (37 loc) · 1.28 KB
/
app.py
File metadata and controls
50 lines (37 loc) · 1.28 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask import Flask, render_template, send_file
import sqlite3
import pandas as pd
from bs4 import BeautifulSoup
import requests
app = Flask(__name__)
# Connecting to SQLite
conn = sqlite3.connect('books.db', check_same_thread=False)
# Delete table if exists
conn.execute('''DROP TABLE IF EXISTS books;''')
# Create table
conn.execute('''CREATE TABLE books(title TEXT, url TEXT);''')
# Web Scraping
url = 'https://books.toscrape.com/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extracting title and url
for article in soup.find_all('article'):
title = article.h3.a.attrs['title']
url = article.h3.a.attrs['href']
# Inserting into SQLite
conn.execute("INSERT INTO books (title, url) VALUES (?, ?)", (title, url))
conn.commit()
books_df = pd.read_sql_query("SELECT * FROM books", conn)
books_df.to_csv("books.csv", index=False)
books_df.to_xml("books.xml", index=False)
# Route to display data
@app.route('/')
def index():
cursor = conn.execute("SELECT title, url from books")
books = cursor.fetchall()
return render_template('index.html', books=books)
@app.route('/download/<string:file_name>')
def download(file_name):
return send_file(file_name, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)