-
Notifications
You must be signed in to change notification settings - Fork 0
/
StockWebParser.py
30 lines (22 loc) · 861 Bytes
/
StockWebParser.py
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
#######
#For Further Reference: https://medium.freecodecamp.org/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe
#######
# import libraries
#use urllib.request instead of urllib2
from bs4 import BeautifulSoup
import urllib.request
import html
# specify the url
quote_page = "http://www.bloomberg.com/quote/SPX:IND"
# query the website and return the html to the variable ‘page’
page = urllib.request.urlopen(quote_page)
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page, "html.parser")
# Take out the <div> of name and get its value
name_box = soup.find("h1", attrs={"class": "name"})
name = name_box.text.strip() # strip() is used to remove starting and trailing
print (name)
# get the index price
price_box = soup.find("div", attrs={"class":"price"})
price = price_box.text
print (price)