【问题标题】:404 Client Error with requests function for yahoo financials404 雅虎财务请求功能的客户端错误
【发布时间】:2021-11-26 06:29:03
【问题描述】:

404 Client Error with requests function for yahoo Financials,直接点击以下网址没问题

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

【问题讨论】:

标签: python beautifulsoup request


【解决方案1】:

你需要注入用户代理

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

【讨论】:

    【解决方案2】:
    1. 只需在您的 get 方法中添加标题,您将获得如下响应 200

    2. 您可以从 First go to chrome developer mode 找到 Headers 和 网络刷新站点然后找到您的 URL 并转到您将找到的标题 所有数据

    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
    

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 1970-01-01
      • 2023-03-15
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多