0% found this document useful (0 votes)
2 views2 pages

python exp 10

The document outlines a web scraping program that extracts quotes from a specified website using the BeautifulSoup library and stores them in an SQLite database. It includes functions for scraping the quotes and for creating a database table to store them. The program executes these functions and confirms successful data storage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

python exp 10

The document outlines a web scraping program that extracts quotes from a specified website using the BeautifulSoup library and stores them in an SQLite database. It includes functions for scraping the quotes and for creating a database table to store them. The program executes these functions and confirms successful data storage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Aim :

Web Scraping : Create a program that uses a web scraping library to extract data from a
website and then stores it in a database.

Code:
import requests
from bs4 import BeautifulSoup
import sqlite3

def scrape_quotes():
url = "http://quotes.toscrape.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

quotes = []
for quote in soup.select('.text'):
quotes.append(quote.get_text())

return quotes

def store_in_database(quotes):
conn = sqlite3.connect('quotes.db')
cursor = conn.cursor()

cursor.execute('''
CREATE TABLE IF NOT EXISTS quotes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
quote TEXT
)
''')

for quote in quotes:


cursor.execute('INSERT INTO quotes (quote) VALUES (?)', (quote,))

conn.commit()
conn.close()
if __name__ == "__main__":
quotes_data = scrape_quotes()

if quotes_data:
store_in_database(quotes_data)
print("Quotes successfully scraped and stored in the database.")
else:
print("Error in scraping quotes.")

Output:

You might also like