株価取得プログラムが動かない
alphavantage APIとか使って、以下のような株アプリを作ってる
原因調査のため、サンプルプログラムを作成して、実行
import mysql.connector
import yfinance as yf
import json
ticker = yf.Ticker("GOOG")
print(ticker.info)
File "/usr/local/lib/python3.9/dist-packages/yfinance/data.py", line 209, in get_raw_json
response.raise_for_status()
File "/usr/local/lib/python3.9/dist-packages/requests/models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/GOOG?modules=summaryProfile%2CfinancialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&ssl=true
確かにエラーになる。。ふむふむ、これは調査せねば
GitHubを調べた
https://github.com/ranaroussi/yfinance/issues/1592
HTTPError: 401 Client Error: Unauthorized for url
とある。これだな。
これはサードパーティのライブラリで、
Yahoo側もいろいろ差し替えるから、いたちごっこなんだろう
対応方法
pip をアップデート
$ pip install yfinance -U
Requirement already satisfied: yfinance in /usr/local/lib/python3.9/dist-packages (0.2.18)
Collecting yfinance
Downloading yfinance-0.2.26-py2.py3-none-any.whl (62 kB)
|????????????????????????????????| 62 kB 705 kB/s
Requirement already satisfied: html5lib>=1.1 in /usr/local/lib/python3.9/dist-packages (from yfinance) (1.1)
Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.9/dist-packages (from yfinance) (0.0.11)
Requirement already satisfied: frozendict>=2.3.4 in /usr/local/lib/python3.9/dist-packages (from yfinance) (2.3.8)
Requirement already satisfied: requests>=2.31 in /usr/local/lib/python3.9/dist-packages (from yfinance) (2.31.0)
Requirement already satisfied: lxml>=4.9.1 in /usr/local/lib/python3.9/dist-packages (from yfinance) (4.9.2)
Requirement already satisfied: numpy>=1.16.5 in /usr/local/lib/python3.9/dist-packages (from yfinance) (1.24.3)
Requirement already satisfied: appdirs>=1.4.4 in /usr/local/lib/python3.9/dist-packages (from yfinance) (1.4.4)
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed yfinance-0.2.26
修正確認
なおった。よかったー。 Googleの株価情報がちゃんと取れている
h$ python3 yfinance_test.py
{'address1': '1600 Amphitheatre Parkway', 'city': 'Mountain View', 'state': 'CA', 'zip': '94043', 'country': 'United States', 'phone': '650 253 0000', 'website': 'https://www.abc.xyz', 'industry':