In [3]:
# Todo:
# 1. Get html from https://www.google.com/finance?q=NASDAQ:AAPL
# 2. Parse html extract the stock price and percent change
# 3. print the results to the user
from bs4 import BeautifulSoup
import requests
def get_html(url):
# Get html from url
response = requests.get(url)
return response.text
def extract_info(html):
# take html extract faculty info return list of dictionaries
soup = BeautifulSoup(html, "lxml")
stock = {
"name": soup.select("div#qwidget_pageheader h1")[0].text,
"price": soup.select("div#qwidget_lastsale")[0].text,
"change": soup.select("div#qwidget_percent")[0].text,
}
return stock
# Follow our steps
symbol = input("Enter Stock Symbol: ")
url = 'http://www.nasdaq.com/symbol/' + symbol
html = get_html(url)
result = extract_info(html)
print("Name: %s" % result["name"])
print("Price: %s" % result["price"])
print("Change: %s" % result["change"])
In [ ]: