![](/img/trans.png)
[英]How to scrape the real time stock price quote using Selenium Python
[英]python stock price real time data feed (script debug)
我剛剛開始學習編碼,如果這個問題是微不足道的,我想提前道歉。
我一直在嘗試找到一種將股市數據實時輸入 python 的方法,並遇到了這個博客http://www.quantatrisk.com/2015/05/07/hacking-google-finance-in-pre-market-trading -Python/
下面是我復制粘貼的腳本。
import urllib2 # works fine with Python 2.7.9 (not 3.4.+)
import json
import time
def fetchPreMarket(symbol, exchange):
link = "http://finance.google.com/finance/info?client=ig&q="
url = link+"%s:%s" % (exchange, symbol)
u = urllib2.urlopen(url)
content = u.read()
data = json.loads(content[3:])
info = data[0]
t = str(info["elt"]) # time stamp
l = float(info["l"]) # close price (previous trading day)
p = float(info["el"]) # stock price in pre-market (after-hours)
return (t,l,p)
p0 = 0
while True:
t, l, p = fetchPreMarket("AAPL","NASDAQ")
if(p!=p0):
p0 = p
print("%s\t%.2f\t%.2f\t%+.2f\t%+.2f%%" % (t, l, p, p-l,
(p/l-1)*100.))
time.sleep(60)
它似乎是一個很棒的代碼,除非我運行它時,我收到以下錯誤消息
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-11-012ec6dc7b0c> in <module>()
18 p0 = 0
19 while True:
---> 20 t, l, p = fetchPreMarket("AAPL","NASDAQ")
21 if(p!=p0):
22 p0 = p
<ipython-input-11-012ec6dc7b0c> in fetchPreMarket(symbol, exchange)
10 data = json.loads(content[3:])
11 info = data[0]
---> 12 t = str(info["elt"]) # time stamp
13 l = float(info["l"]) # close price (previous trading day)
14 p = float(info["el"]) # stock price in pre-market (after-hours)
KeyError: 'elt'
我嘗試修改 fetchPreMarket 使其僅輸出info = data[0]
但是當我嘗試“打印信息”時,什么也沒有。
提前致謝
所以... Google Finance API 已停產 。 我很驚訝鏈接有效,但是數據中沒有"etl"
(或"el"
)鍵。
因此,您將在info["elt"]
處收到KeyError: 'elt'
以供參考,
{
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "95.52"
,"l_fix" : "95.52"
,"l_cur" : "95.52"
,"s": "0"
,"ltt":"2:34PM EST"
,"lt" : "Feb 24, 2:34PM EST"
,"lt_dts" : "2016-02-24T14:34:54Z"
,"c" : "+0.83"
,"c_fix" : "0.83"
,"cp" : "0.88"
,"cp_fix" : "0.88"
,"ccol" : "chg"
,"pcls_fix" : "94.69"
}
與googlefinance或yahoo-finance python模塊相比,您可能會更好,而不是直接調用API地址。
值得注意的是,雅虎API並非“實時”。 大約延遲15分鍾報價。
如果您不想使用googlefinance
package,您可以使用BeautifulSoup
創建自己的解析器。
在使用 BeautifulSoup 的在線 IDE 中檢查代碼。
from bs4 import BeautifulSoup
import requests, json, lxml, re
from itertools import zip_longest
def scrape_google_finance(ticker: str):
# https://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls
params = {
"hl": "en" # language
}
# https://docs.python-requests.org/en/master/user/quickstart/#custom-headers
# https://www.whatismybrowser.com/detect/what-is-my-user-agent
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36",
}
html = requests.get(f"https://www.google.com/finance/quote/{ticker}", params=params, headers=headers, timeout=30)
soup = BeautifulSoup(html.text, "lxml")
ticker_data = {"ticker_info": {},
"right_panel_data": {},
"finance_perfomance": {"table": []}
}
ticker_data["ticker_info"]["title"] = soup.select_one(".zzDege").text
ticker_data["ticker_info"]["current_price"] = soup.select_one(".AHmHk .fxKbKc").text
right_panel_keys = soup.select(".gyFHrc .mfs7Fc")
right_panel_values = soup.select(".gyFHrc .P6K39c")
for key, value in zip_longest(right_panel_keys, right_panel_values):
key_value = key.text.lower().replace(" ", "_")
ticker_data["right_panel_data"][key_value] = value.text
if soup.select(".slpEwd .roXhBd"):
# https://regex101.com/r/Sf2TaC/1
fin_perf_col_2 = re.search(r"\w+\s?\d{4}", soup.select_one(".PFjsMe+ .yNnsfe").text).group(0) # e.g. JUN 2022
fin_perf_col_3 = soup.select_one(".PFjsMe~ .yNnsfe+ .yNnsfe").text # e.g. Year/year change
for fin_perf in soup.select(".slpEwd .roXhBd"):
if fin_perf.select(".rsPbEe"):
perf_key = fin_perf.select_one(".rsPbEe").text # e.g. Revenue, Net Income, Operating Income..
perf_value_col_1 = fin_perf.select_one(".QXDnM").text # 48.23B, ...
try:
perf_value_col_2 = fin_perf.select_one(".gEUVJe .JwB6zf").text # -21.82%, ...
except:
perf_value_col_2 = None #
ticker_data["finance_perfomance"]["table"].append({
perf_key: {
fin_perf_col_2: perf_value_col_1,
fin_perf_col_3: perf_value_col_2
}
})
else:
ticker_data["finance_perfomance"]["error"] = f"No 'finence perfomance table' for {ticker}."
return ticker_data
data = scrape_google_finance(ticker="AAPL:NASDAQ")
print(json.dumps(data, indent=2))
示例 output
[
{
"ticker_info": {
"title": "Apple Inc",
"current_price": "$174.55"
},
"right_panel_data": {
"previous_close": "$173.03",
"day_range": "$172.57 - $176.15",
"year_range": "$129.04 - $182.94",
"market_cap": "2.81T USD",
"avg_volume": "68.49M",
"p/e_ratio": "28.84",
"dividend_yield": "0.53%",
"primary_exchange": "NASDAQ",
"ceo": "Tim Cook",
"founded": "Apr 1, 1976",
"headquarters": "Cupertino, CaliforniaUnited States",
"website": "apple.com",
"employees": "154,000"
},
"finance_perfomance": {
"table": [
{
"Revenue": {
"Jun 2022": "82.96B",
"Y/Y change": "1.87%"
}
},
{
"Operating expense": {
"Jun 2022": "12.81B",
"Y/Y change": "15.10%"
}
},
{
"Net income": {
"Jun 2022": "19.44B",
"Y/Y change": "-10.59%"
}
# ...
]
如果您需要從 Google Finance 中抓取更多數據,請參閱 Python 博客文章中的抓取 Google Finance Ticker Quote Data 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.