Created
September 11, 2019 14:27
-
-
Save crimeminister/d6997da708a2baea929146cb095e8575 to your computer and use it in GitHub Desktop.
Stock scraper for Yahoo! Finance
This file contains 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
bs4 | |
requests |
This file contains 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
from requests import get | |
from bs4 import BeautifulSoup | |
from pprint import PrettyPrinter | |
pp = PrettyPrinter(indent=4) | |
url = 'https://finance.yahoo.com/quote/CNR.TO/key-statistics?p=CNR.TO' | |
response = get(url) | |
soup = BeautifulSoup(response.text,'html.parser') | |
headings = [ | |
'Valuation Measures', | |
'Financial Highlights', | |
'Trading Information', | |
] | |
data = {} | |
for heading in headings: | |
header = soup.find('h2', string=heading) | |
tables = header.parent.find_all('table') | |
for table in tables: | |
rows = table.find_all('tr') | |
for row in rows: | |
label,value = row.find_all('td') | |
data[label.text] = value.text | |
pp.pprint(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment