雅虎财务的请求功能出现 404 客户端错误

404 Client Error with requests function for yahoo financials

404 Client Error with requests function for yahoo financials,直接点下面URL没问题

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

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

结果

HTTPError: 404 Client Error: Not Found for url: https://finance.yahoo.com/quote/AAPL/financials?p=AAPL
  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

输出:

200

您需要注入用户代理

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