-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuotes_crawler.py
More file actions
29 lines (23 loc) · 791 Bytes
/
Quotes_crawler.py
File metadata and controls
29 lines (23 loc) · 791 Bytes
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
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 = [] #list of dictionaries representing quotes
table = soup.find('div', attrs = {'id':'portfolio'})
for row in table.findAll('div', attrs = {'class':'portfolio-image'}):
quote = {} #dictionary
#quote['theme'] = row.h5.text
quote['url'] = row.a['href']
quote['img'] = row.img['src']
quote['lines'] = row.img['alt']
#quote['author'] = row.p.text
quotes.append(quote)
filename = 'Quotes.csv'
with open(filename, 'wb') as f:
#w = csv.DictWriter(f, ['theme', 'url', 'img', 'lines', 'author'])
w = csv.DictWriter(f, ['url', 'img', 'lines'])
w.writeheader()
for quote in quotes:
w.writerow(quote)