如何从yahoo_finance股票中检索实时价格?

时间:2017-12-10 15:09:23

标签: python web-scraping beautifulsoup yahoo-finance

我正在构建一个应用程序,我想从yahoo-finance中检索最新的报价。我试图使用美丽的汤废弃路径,但是当我打印时我得到的是一个空列表。有什么建议? (outter html:169.37)

 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)

3 个答案:

答案 0 :(得分:1)

发现了一个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'  

猜测,这将更容易使用并减少

答案 1 :(得分:0)

您可以将selenium与BeautifulSoup结合使用,以获取您所追求的内容。如下所示:

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()

输出:

169.37

答案 2 :(得分:0)

您可以使用yahoo_fin软件包(请参见http://theautomatic.net/yahoo_fin-documentation/)。这是几个例子:

# 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")

只需将“ AAPL”或“ AMZN”替换为所需的股票代码即可。