File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments