May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
|
||||
#Python program to scrape website
|
||||
#and save quotes from website
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import csv
|
||||
|
||||
URL = "http://www.values.com/inspirational-quotes"
|
||||
r = requests.get(URL)
|
||||
|
||||
soup = BeautifulSoup(r.content, 'html5lib')
|
||||
|
||||
quotes=[] # a list to store quotes
|
||||
|
||||
table = soup.find('div', attrs = {'id':'all_quotes'})
|
||||
|
||||
for row in table.findAll('div',
|
||||
attrs = {'class':'col-6 col-lg-3 text-center margin-30px-bottom sm-margin-30px-top'}):
|
||||
quote = {}
|
||||
quote['theme'] = row.h5.text
|
||||
quote['url'] = row.a['href']
|
||||
quote['img'] = row.img['src']
|
||||
quote['lines'] = row.img['alt'].split(" #")[0]
|
||||
quote['author'] = row.img['alt'].split(" #")[1]
|
||||
quotes.append(quote)
|
||||
print(quote)
|
||||
|
||||
filename = 'inspirational_quotes.csv'
|
||||
with open(filename, 'w', newline='') as f:
|
||||
w = csv.DictWriter(f,['theme','url','img','lines','author'])
|
||||
w.writeheader()
|
||||
for quote in quotes:
|
||||
print(quote)
|
||||
w.writerow(quote)
|
||||
Reference in New Issue
Block a user