'404 Client Error with requests function for yahoo financials

404 Client Error with requests function for yahoo financials, direct click the following URL is no problem

https://finance.yahoo.com/quote/AAPL/financials?p=AAPL

import requests
a = requests.get('https://finance.yahoo.com/quote/AAPL/financials?p=AAPL')
a.raise_for_status()

OR

import urllib
req = urllib.request.urlopen("https://finance.yahoo.com/quote/AAPL/financials?p=AAPL")
data = req.read()

result

HTTPError: 404 Client Error: Not Found for url: https://finance.yahoo.com/quote/AAPL/financials?p=AAPL


Solution 1:[1]

You need to inject user agent

import requests

headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'}
    
a = requests.get('https://finance.yahoo.com/quote/AAPL/financials?p=AAPL',headers= headers)
a.raise_for_status()

Solution 2:[2]

  1. Just add the Headers in your get method you will get response as 200

  2. You can find Headers from First go to chrome developer mode and Network refresh site then find your URL and go to Headers you will find all data

import requests
headers={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"}
res=requests.get("https://finance.yahoo.com/quote/AAPL/financials?p=AAPL",headers=headers)
res.status_code

Output:

200

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 F.Hoque
Solution 2 Bhavya Parikh