pythonbeautifulsouprequest

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

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