Skip to content

Instantly share code, notes, and snippets.

@crimeminister
Created September 11, 2019 14:27
Show Gist options
  • Save crimeminister/d6997da708a2baea929146cb095e8575 to your computer and use it in GitHub Desktop.
Save crimeminister/d6997da708a2baea929146cb095e8575 to your computer and use it in GitHub Desktop.
Stock scraper for Yahoo! Finance
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