0

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)
bouteillebleu
  • 2,456
  • 23
  • 32
brain_dead_cow
  • 83
  • 5
  • 13

3 Answers3

1

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
SIM
  • 21,997
  • 5
  • 37
  • 109
  • I am currently using visual studio and i get an error with bs4 although i have got it installed. Do you know why is this happening? – brain_dead_cow Dec 10 '17 at 21:59
  • When it comes to give any suggestion about installing stuff, my head spins. You can google to find any solution as to how you can get `bs4` working. Btw, the above piece of code is worth trying for. Thanks. – SIM Dec 10 '17 at 22:07
  • It is working almost perfectly. I just have a question. When i run the code a Chrome webpage pops up with the url i provided in the code above. How can i stop this?? I just want to get the number. Thank you – brain_dead_cow Dec 10 '17 at 22:28
  • in that case you need to choose `headless chrome driver`, `phantomjs` or drivers like that. However, there are some issues in those headless drivers when it comes to deal with click event or infinite scrolling webpages. – SIM Dec 10 '17 at 22:42
  • from selenium.webdriver.chrome.options import Options opts = Options() opts.set_headless() driver = webdriver.Chrome(options=opts); you can use this method to scrape data without pops up – user466130 May 24 '18 at 15:01
1

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

Mr. Cat
  • 55
  • 3
0

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.

atreadw
  • 289
  • 2
  • 6