简体   繁体   English

Python 下载 zip 文件以损坏的文件结尾

[英]Python download zip file ends in corrupted file

I've tried various ways to download the zip file at this location: https://www.dupageresults.gov//IL/DuPage/115972/314432/reports/summary.zip我尝试了多种方法在此位置下载 zip 文件: https://www.dupageresults.gov//IL/DuPage/115972/314432/reports/summary.zip

I can't seem to get the full uncorrupted file with Requests and can't seem to find the correct answer.我似乎无法通过 Requests 获得完整且未损坏的文件,并且似乎无法找到正确的答案。 I've tried a.click() on the xpath as well and that doesn't give me the full file either.我也在 xpath 上尝试了 a.click() ,但也没有给我完整的文件。

I can click on it manually and get an uncorrupted file but any Python method doesn't seem to work for me.我可以手动单击它并获得一个未损坏的文件,但任何 Python 方法似乎对我都不起作用。 Any idea what's happening here?知道这里发生了什么吗?

clicktodownload = driver.find_element(By.XPATH, value = '//div[@class="card contest-loader"]').click() clicktodownload = driver.find_element(By.XPATH, value = '//div[@class="card contest-loader"]').click()

I believe my issue was I didn't give the download enough time before I applied the next method to it which was causing a corrupt zip.我相信我的问题是在应用下一个方法之前我没有给下载足够的时间,这导致了损坏的 zip。

Try using python requests as following:尝试使用 python 请求如下:

import requests
import shutil

def download_file(url):
    local_filename = url.split('/')[-1]
    with requests.get(url, stream=True) as r:
        with open(local_filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)

    return local_filename

download_file("https://www.dupageresults.gov//IL/DuPage/115972/314432/reports/summary.zip")

resource资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM