-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_web
More file actions
31 lines (22 loc) · 790 Bytes
/
scrape_web
File metadata and controls
31 lines (22 loc) · 790 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
30
'''
'''
import urllib.request as urllib2
from bs4 import BeautifulSoup
import csv
from datetime import datetime
# Bloomberg URL
quote_page = 'http://www.bloomberg.com/quote/SPX:IND'
# Query the website and return the HTML to the variable page
page = urllib2.urlopen(quote_page)
# parse the html using beautiful soup and store in a variable called soup
soup = BeautifulSoup(page, 'html.parser')
name_box = soup.find('h1', attrs = {'class': 'name'})
name = name_box.text.strip()
print(name)
price_box = soup.find('div', attrs = {'class': 'price'})
price = price_box.text
print(price)
# Open a csv file with append, so old data will not be erased
with open('sp_index.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([name, price, datetime.now()])