-
-
Save ChrisAllisonMalta/31040feea4b90bbac1ecb93ce323f16a to your computer and use it in GitHub Desktop.
Scrape stock price from google finance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python 3.4 | |
import urllib | |
import bs4 | |
def get_stock_price(name): | |
url = "https://www.google.com/finance" | |
req = urllib.request.Request(url) | |
url_full = url + "?" + urllib.parse.urlencode({"q": name}) | |
req = urllib.request.Request(url_full) | |
resp = urllib.request.urlopen(req) | |
data = resp.read() | |
bs = bs4.BeautifulSoup(data) | |
div_price = bs.find(attrs={"id": "price-panel"}) | |
span_price = div_price.find(attrs={"class": "pr"}) | |
return float(span_price.text.strip()) | |
print(get_stock_price("GOOG")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment