Get the current price of Apple inc. Stock

Google NASDAQ:AAPL, scrape the current trading price and the percent change for today from www.nasdaq.com


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"])


Enter Stock Symbol: AMZN
Name: Amazon.com, Inc. Common Stock Quote & Summary Data
Price: $899.8076
Change: 0.25%

In [ ]: