株価取得プログラムが動かない
原因調査のため、サンプルプログラムを作成して、実行
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)
Requirement already satisfied: beautifulsoup4>=4.11.1 in /usr/local/lib/python3.9/dist-packages (from yfinance) (4.12.2)
Requirement already satisfied: pandas>=1.3.0 in /usr/local/lib/python3.9/dist-packages (from yfinance) (2.0.2)
Requirement already satisfied: pytz>=2022.5 in /usr/local/lib/python3.9/dist-packages (from yfinance) (2023.3)
Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.9/dist-packages (from beautifulsoup4>=4.11.1->yfinance) (2.4.1)
Requirement already satisfied: six>=1.9 in /usr/lib/python3/dist-packages (from html5lib>=1.1->yfinance) (1.16.0)
Requirement already satisfied: webencodings in /usr/local/lib/python3.9/dist-packages (from html5lib>=1.1->yfinance) (0.5.1)
Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.3.0->yfinance) (2.8.2)
Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.3.0->yfinance) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.9/dist-packages (from requests>=2.31->yfinance) (3.1.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3/dist-packages (from requests>=2.31->yfinance) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests>=2.31->yfinance) (2020.6.20)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/lib/python3/dist-packages (from requests>=2.31->yfinance) (1.26.5)
Installing collected packages: yfinance
WARNING: The script sample is installed in '/home/hiroshi855/.local/bin' which is not on PATH.
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
修正確認
なおった。よかったー
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': 'Internet Content & Information', 'industryDisp': 'Internet Content & Information', 'sector': 'Communication Services', 'longBusinessSummary': 'Alphabet Inc. offers various products and platforms in the United States, Europe, the Middle East, Africa, the Asia-Pacific, Canada, and Latin America. It operates through Google Services, Google Cloud, and Other Bets segments. The Google Services segment provides products and services, including ads, Android, Chrome, hardware, Gmail, Google Drive, Google Maps, Google Photos, Google Play, Search, and YouTube. It is also involved in the sale of apps and in-app purchases and digital content in the Google Play store; and Fitbit wearable devices, Google Nest home products, Pixel phones, and other devices, as well as in the provision of YouTube non-advertising services. The Google Cloud segment offers infrastructure, cybersecurity, data, analytics, AI, and machine learning, and other services; Google Workspace that include cloud-based collaboration tools for enterprises, such as Gmail, Docs, Drive, Calendar, and Meet; and other services for enterprise customers. The Other Bets segment sells health technology and internet services. The company was founded in 1998 and is headquartered in Mountain View, California.', 'fullTimeEmployees': 190711, 'companyOfficers': [{'maxAge': 1, 'name': 'Mr. Sundar Pichai', 'age': 49, 'title': 'CEO & Director', 'yearBorn': 1973, 'fiscalYear': 2022, 'totalPay': 7947461, 'exercisedValue': 39618220, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Lawrence Edward Page', 'age': 49, 'title': 'Co-Founder & Director', 'yearBorn': 1973, 'fiscalYear': 2022, 'totalPay': 1, 'exercisedValue': 0