0

直到最近我才能够连接到雅虎财经,这让我可以用 json-data 填充我的股票筛选器。但是,几周后,我无法再连接到雅虎财经。显然,雅虎财经阻止了访问其数据的传统方式,我想知道是否有办法重新建立这种连接。

以下是我如何连接到 Yahoo Finance 的一些技术细节(在过去 2 年中没有任何问题):

r = requests.get(url_root_yahoo + ticker + key_stats_yahoo + ticker)
data = json.loads(p.findall(r.text)[0])
quote_store = data['context']['dispatcher']['stores']['QuoteSummaryStore']
statistics = quote_store['defaultKeyStatistics']
profit_margin = statistics['profitMargins']['raw']

但现在我收到以下错误消息:

File "stock_screener_4.py", line 80, in <module>
quote_store = data['context']['dispatcher']['stores']['QuoteSummaryStore']
NameError: name 'data' is not defined

任何提示如何重新建立连接?

非常感谢。

PS 最小工作示例:

import os
import urllib.request
import requests, re, json

p = re.compile(r'root\.App\.main = (.*);')

url_root_yahoo = 'https://finance.yahoo.com/quote/'
key_stats_yahoo = '/key-statistics?p='
ticker = 'AAPL'
execute_next_block = 1

r = requests.get(url_root_yahoo + ticker + key_stats_yahoo + ticker)

try:
    data = json.loads(p.findall(r.text)[0])
except (KeyError, IndexError, TypeError):
    execute_next_block = 0

try:
    quote_store = data['context']['dispatcher']['stores']['QuoteSummaryStore']
    statistics = quote_store['defaultKeyStatistics']
except (KeyError, IndexError, TypeError):
     execute_next_block = 0
4

0 回答 0