问题
I've been using the yahoo finance module in python. I put the project down for the last couple months and just started it up again. Unfortunately it doesn't seem that there is anything behind the yahoo finance module anymore!
I run the sample code from the pypi documents and get nothing back.
>>> from yahoo_finance import Share
>>> yahoo = Share('YHOO')
>>> print yahoo.get_open()
None
>>> print yahoo.get_price()
None
>>> print yahoo.get_trade_datetime()
None
Although I was able to open for GOOG I get an error when I try to access historical data, and cannot seem to access historical data for any stock.
None
>>> goog = Share('GOOG')
>>> goog.get_open()
'956.83'
>>> print(yahoo.get_open())
None
>>> goog.get_historical('2014-04-25', '2014-04-29')
Traceback (most recent call last):
File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\yahoo_finance\__init__.py", line 120, in _request
_, results = response['query']['results'].popitem()
AttributeError: 'NoneType' object has no attribute 'popitem'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\yahoo_finance\__init__.py", line 123, in _request
raise YQLQueryError(response['error']['description'])
KeyError: 'error'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\yahoo_finance\__init__.py", line 342, in get_historical
result = self._request(query)
File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\yahoo_finance\__init__.py", line 125, in _request
raise YQLResponseMalformedError()
yahoo_finance.YQLResponseMalformedError: Response malformed.
>>>
I've tried running with a more recent date range and no luck. Historical data seems to be simply unavailable from the module, where it was dependable just a few weeks ago. Any insights?
回答1:
From Ed0906's answer here: https://stackoverflow.com/a/44050039/5766416
Yahoo discontinued their API for desktop connections. You can still connect to it via your phone. So one way you can circumvent this is by setting the headers of your GET request to some mobile browser.
OR
Follow Ed's method by getting and setting the crumb in your requests. I used it, and verified it works.
来源:https://stackoverflow.com/questions/44753963/yahoo-finance-module-doesnt-work-anymore