404 Client Error with requests function for yahoo financials

Viewed 271

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
2 Answers

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()
  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
Related