-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get_news() returns news not related to the given ticker #1956
Comments
I see this issue as well.
Returns "random" news or news from the main page. Edit: Making the raw API call also returns the same faulty news. Looks like the error stems from the URL endpoint. |
Same issue, hopefully they will fix the endpoint soon |
straight up doesnt work now? |
Yes, had that yesterday. Today it worked for a while and now it is giving this error again. |
I believe this issue arose from @utils.log_indent_decorator
def get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30):
# Important: treat input arguments as immutable.
if len(url) > 200:
utils.get_yf_logger().debug(f'url={url[:200]}...')
else:
utils.get_yf_logger().debug(f'url={url}')
utils.get_yf_logger().debug(f'params={params}')
proxy = self._get_proxy(proxy)
if params is None:
params = {}
if 'crumb' in params:
raise Exception("Don't manually add 'crumb' to params dict, let data.py handle it")
cookie, crumb, strategy = self._get_cookie_and_crumb()
if crumb is not None:
crumbs = {'crumb': crumb}
else:
crumbs = {}
if strategy == 'basic' and cookie is not None:
# Basic cookie strategy adds cookie to GET parameters
cookies = {cookie.name: cookie.value}
else:
cookies = None
request_args = {
'url': url,
'params': {**params, **crumbs},
'cookies': cookies,
'proxies': proxy,
'timeout': timeout,
'headers': user_agent_headers or self.user_agent_headers
}
response = self._session.get(**request_args)
utils.get_yf_logger().debug(f'response code={response.status_code}')
if response.status_code >= 400:
# Retry with other cookie strategy
if strategy == 'basic':
self._set_cookie_strategy('csrf')
else:
self._set_cookie_strategy('basic')
cookie, crumb, strategy = self._get_cookie_and_crumb(proxy, timeout)
request_args['params']['crumb'] = crumb
if strategy == 'basic':
request_args['cookies'] = {cookie.name: cookie.value}
response = self._session.get(**request_args)
utils.get_yf_logger().debug(f'response code={response.status_code}')
return response Adding a second status code check would prevent the error from being raised, and perhaps a wait period or backoff loop before going into the second strategy. |
I noticed that for this particular ticker t = "1A9.F" # Using the original ticker for the first try, which gives front-page news
r = requests.get(
url = f"https://query2.finance.yahoo.com/v1/finance/search?q={t}",
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}
)
data = r.json()
print(data)
>>> ... 'news': [{'uuid': '03ab3a06-2fca-3f0b-a94e-7728b5db1ea0',
'title': "Parks Canada and partners take first step toward creating a national urban park in St. John's",
'publisher': 'CNW Group',
'link': 'https://finance.yahoo.com/news/parks-canada-partners-first-step-204500140.html',
'providerPublishTime': 1720471500,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/zXZUNStn6b5.lW8TIWS5MA--~B/aD0zMDA7dz00MDA7YXBwaWQ9eXRhY2h5b24-/https://media.zenfs.com/en/cnwgroup.com/bad4812fef6c57928122ed62b23bf521',
'width': 400,
'height': 300,
'tag': 'original'},
{'url': 'https://s.yimg.com/uu/api/res/1.2/3wPCwm4YhXri9JmUAow1Rg--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/cnwgroup.com/bad4812fef6c57928122ed62b23bf521',
'width': 140,
'height': 140,
'tag': '140x140'}]}},
{'uuid': 'd791484e-8417-3aae-97fc-273446fc2995',
'title': 'Tesla Stock’s Win Streak Hits 9 Days. It Almost Didn’t Happen.',
'publisher': 'Barrons.com',
'link': 'https://finance.yahoo.com/m/d791484e-8417-3aae-97fc-273446fc2995/tesla-stock%E2%80%99s-win-streak-hits.html',
'providerPublishTime': 1720471380,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/rHL2F56x3R2OPgKJZ_LqSQ--~B/aD02NDA7dz0xMjgwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/Barrons.com/e4a0d77b5267671e4800b437969ca49d',
'width': 1280,
'height': 640,
'tag': 'original'} ... t2 = data['quotes'][0]['longname'] # If use the longname from the previous quote instead, it works
r2 = requests.get(
url = f"https://query2.finance.yahoo.com/v1/finance/search?q={t2}",
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}
)
data2 = r2.json()
print(data2)
>>> ... {'uuid': 'bae62caa-2116-3725-8a35-7e5c5ffc0487',
'title': 'Asante Files Financial and Operating Results for the Quarter Ended April 30, 2024',
'publisher': 'GlobeNewswire',
'link': 'https://finance.yahoo.com/news/asante-files-financial-operating-results-110000900.html',
'providerPublishTime': 1718622000,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/IoEIOReUphFfMjBXXQ2w.g--~B/aD00Mzg7dz05Njg7YXBwaWQ9eXRhY2h5b24-/https://media.zenfs.com/en/globenewswire.com/ad2b25f2467b60b1f2b65ac649423e4a',
'width': 968,
'height': 438,
'tag': 'original'},
{'url': 'https://s.yimg.com/uu/api/res/1.2/FAPh5S6ry5TgLH1N90GGkA--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/globenewswire.com/ad2b25f2467b60b1f2b65ac649423e4a',
'width': 140,
'height': 140,
'tag': '140x140'}]},
'relatedTickers': ['ASGOF']},
{'uuid': '405254d2-bde0-3fd9-9f69-8d00a37067bf',
'title': 'Asante Announces Project Update for the Sulphide Recovery Plant at the Bibiani Gold Mine',
'publisher': 'GlobeNewswire',
'link': 'https://finance.yahoo.com/news/asante-announces-project-sulphide-recovery-111500095.html',
'providerPublishTime': 1718277300,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/IoEIOReUphFfMjBXXQ2w.g--~B/aD00Mzg7dz05Njg7YXBwaWQ9eXRhY2h5b24-/https://media.zenfs.com/en/globenewswire.com/ad2b25f2467b60b1f2b65ac649423e4a',
'width': 968,
'height': 438,
'tag': 'original'},
{'url': 'https://s.yimg.com/uu/api/res/1.2/FAPh5S6ry5TgLH1N90GGkA--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/globenewswire.com/ad2b25f2467b60b1f2b65ac649423e4a',
'width': 140,
'height': 140,
'tag': '140x140'}]},
'relatedTickers': ['ASGOF']},
{'uuid': '347918dd-bc9b-3853-9de1-6d2afb247150',
'title': 'Asante Announces Rerouting of Bibiani-Goaso Highway, Paving the Way to Grow Gold Production at the Bibiani Gold Mine',
'publisher': 'GlobeNewswire',
'link': 'https://finance.yahoo.com/news/asante-announces-rerouting-bibiani-goaso-110000022.html',
'providerPublishTime': 1718276400,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/IoEIOReUphFfMjBXXQ2w.g--~B/aD00Mzg7dz05Njg7YXBwaWQ9eXRhY2h5b24-/https://media.zenfs.com/en/globenewswire.com/ad2b25f2467b60b1f2b65ac649423e4a',
'width': 968,
'height': 438,
'tag': 'original'},
{'url': 'https://s.yimg.com/uu/api/res/1.2/FAPh5S6ry5TgLH1N90GGkA--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/globenewswire.com/ad2b25f2467b60b1f2b65ac649423e4a',
'width': 140,
'height': 140,
'tag': '140x140'}]},
'relatedTickers': ['ASGOF']} ... I think it might be worth looking into adding a recursive search method, but it doesn't work every time. For instance, the same strategy fails when used with |
Describe bug
I used yfinance to get news from different companies listed on various stock exchanges using method news (.get_news()) and it worked very well.
However, starting from March 2024 the get_news() method returns news not related to given ticker. I believe it now returns news from the main page of Yahoo Finance (see Bad data proof. For example, I asked about "1A9.F" ticker and it returned 'relatedTickers': ['CMOPF'] news)
Simple code that reproduces your problem
Debug log
Bad data proof
yfinance
version0.2.40
Python version
3.10
Operating system
Windows
The text was updated successfully, but these errors were encountered: