Reputation: 83
I am building an application and i want to retrieve the latest quoted price from yahoo-finance. I am trying to scrape the path using BeautifulSoup, however all I get when I print is an empty list. Any suggestions?
Example HTML:
<span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="35"><!-- react-text: 36 -->169.37<!-- /react-text --></span>
My code:
import requests
from bs4 import BeautifulSoup
a = requests.get('https://finance.yahoo.com/quote/AAPL/history?p=AAPL')
soup = BeautifulSoup(a.content, 'lxml')
search = soup.find_all('span', {'class':'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px)
D(ib)'})
print(search)
Upvotes: 0
Views: 7805
Reputation: 299
You can use the yahoo_fin package (see http://theautomatic.net/yahoo_fin-documentation/). Here's a couple examples:
# load the stock_info module from yahoo_fin
from yahoo_fin import stock_info as si
# get Apple's live quote price
si.get_live_price("AAPL")
# or Amazon's
si.get_live_price("AMZN")
Just replace "AAPL" or "AMZN" with whatever ticker you need.
Upvotes: 0
Reputation: 55
found a python api:
https://pypi.python.org/pypi/yahoo-finance
>>> from yahoo_finance import Share
>>> yahoo = Share('YHOO')
>>> print yahoo.get_open()
'36.60'
>>> print yahoo.get_price()
'36.84'
>>> print yahoo.get_trade_datetime()
'2014-02-05 20:50:00 UTC+0000'
at a guess, this will be easier to use and break less
Upvotes: 1
Reputation: 22440
You can use selenium in combination with BeautifulSoup to get the content you are after. Something like below:
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://finance.yahoo.com/quote/AAPL/history?p=AAPL')
soup = BeautifulSoup(driver.page_source,"lxml")
item = soup.find(id="quote-market-notice").find_parent().find("span").text
print(item)
driver.quit()
Output:
169.37
Upvotes: 1