【问题标题】:Downloading zip file using Python give HTTP 403使用 Python 下载 zip 文件给出 HTTP 403
【发布时间】:2021-01-19 12:43:32
【问题描述】:

我正在使用 Python 中的请求模块从 Internet 下载 zip 文件。 我在尝试这样做时遇到 403 错误。

该文件遵循服务器中的模式,因此我能够动态生成 url。它一直工作到 2020 年 12 月 31 日。但不是在这之后。

可用网址:https://www1.nseindia.com/content/historical/EQUITIES/2020/DEC/cm31DEC2020bhav.csv.zip 不可行的网址:https://www1.nseindia.com/content/historical/EQUITIES/2021/JAN/cm01JAN2021bhav.csv.zip

Python 代码:

formattedUrl='https://www1.nseindia.com/content/historical/EQUITIES/2021/JAN/cm01JAN2021bhav.csv.zip'
requestedFile = requests.get(formattedUrl)
requestedFile.status_code

也试过这个:

from fake_useragent import UserAgent
import requests

ua_str = UserAgent().chrome
formattedUrl='https://www1.nseindia.com/content/historical/EQUITIES/2021/JAN/cm01JAN2021bhav.csv.zip'
requestedFile = requests.get(formattedUrl,headers={"User-Agent": ua_str})
requestedFile.status_code

也试过这个:

import zipfile, urllib.request, shutil

url = formattedUrl
file_name = 'cm01JAN2021bhav.csv.zip'

with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
    shutil.copyfileobj(response, out_file)
    with zipfile.ZipFile(file_name) as zf:
        zf.extractall()

它们都没有工作。 任何建议。

问候。

编辑:添加更多详细信息,因为某些 cmets 表示不允许下载文件或需要某种身份验证。 我可以通过那里的页面下载它。不需要身份验证。 访问:https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm 选择报告:Bhavcopy 日期:01-01-2021 点击获取数据 您将获得文件链接,单击该链接将下载文件。 截屏:

【问题讨论】:

  • 这与 Python 没有任何关系。服务器根本不愿意提供该文件:要么它实际上不存在,要么它需要某种形式的身份验证才能访问它。尝试在浏览器中访问该 URL,您将收到相同的错误。
  • 错误 403 表示您无权访问该文件
  • 我已经编辑了我的问题。请检查。基本上您无需任何身份验证即可下载。您可以按照我提到的步骤操作,
  • 现在是 3。单击您在上面提供的两个 URL 会在浏览器中重现问题。第一个链接有效。第二个没有。所以这是相关网站的问题,与编写 HTTP 客户端无关。

标签: python zipfile


【解决方案1】:

有类似的问题,下面的代码解决了我的问题,猜你需要点击 2 个额外的 URL 并使用它返回的 cookie

import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63',
            'Upgrade-Insecure-Requests':'1',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'en-US,en;q=0.9',
            'Connection': 'keep-alive',
            'Host': 'www1.nseindia.com',
            'Referer':'https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin'}
resp1 = requests.get('https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm',headers=headers)
print(resp1)
resp2 = requests.get('https://www1.nseindia.com/ArchieveSearch?h_filetype=eqbhav&date=05-01-2021&section=EQ',headers=headers,cookies=resp1.cookies)
print(resp2)
resp3 = requests.get('https://www1.nseindia.com/content/historical/EQUITIES/2021/JAN/cm05JAN2021bhav.csv.zip',headers=headers)
print(resp3.content)
with open('c:/temp/test.zip', 'wb') as handle:
    for block in resp3.iter_content(1024):
        if not block:
            break
        handle.write(block)

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多