Skip to content

Commit b51a496

Browse files
committed
feat: Added program to get quotes from website.
1 parent d11db57 commit b51a496

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Random Quotes Generator/quotes.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from bs4 import BeautifulSoup
2+
import requests
3+
import csv
4+
5+
url='http://quotes.toscrape.com'
6+
7+
html=requests.get(url)
8+
bs=BeautifulSoup(html.text,'html.parser')
9+
10+
try:
11+
csv_file=open('quote_list.csv','w')
12+
fieldnames=['quote','author','tags']
13+
dictwriter=csv.DictWriter(csv_file,fieldnames=fieldnames)
14+
15+
dictwriter.writeheader()
16+
17+
while True:
18+
for quote in bs.findAll('div',{'class':'quote'}):
19+
text=quote.find('span',{'class':'text'}).text
20+
author=quote.find('small',{'class':'author'}).text
21+
tags=[]
22+
for tag in quote.findAll('a',{'class':'tag'}):
23+
tags.append(tag.text)
24+
dictwriter.writerow({'quote':text,'author':author,'tags':tags})
25+
26+
next=bs.find('li',{'class':'next'})
27+
if not next:
28+
break
29+
30+
html=requests.get(url+next.a.attrs['href'])
31+
bs=BeautifulSoup(html.text,'html.parser')
32+
except:
33+
print('Unknown Error!!!')
34+
finally:
35+
csv_file.close()

0 commit comments

Comments
 (0)