下载的时候图片很大

Images VERY LARGE when downloading

我正在尝试从网站下载一些图片。当我用浏览器下载它们时,它们比用我 code.They 下载的文件小得多,与用我的代码下载的文件具有相同的分辨率,但文件大小之间的差异非常大!

def download(url, pathname):
    """
    Downloads a file given an URL and puts it in the folder `pathname`
    """
    # if path doesn't exist, make that path dir
    if not os.path.isdir(pathname):
        os.makedirs(pathname)
    # download the body of response by chunk, not immediately
    response = requests.get(url, stream=True)

    # get the total file size
    file_size = int(response.headers.get("Content-Length", 0))

    # get the file name
    filename = os.path.join(pathname, url.split("/")[-1])


        # progress bar, changing the unit to bytes instead of iteration (default by tqdm)
    progress = tqdm(response.iter_content(1024), f"Downloading {filename}", total=file_size, unit="B", unit_scale=True,
                        unit_divisor=1024, disable=True)

    with open(filename, "wb") as f:
        for data in progress.iterable:
            # write data read to the file
            f.write(requests.get(url).content)
            # update the progress bar manually
            progress.update(len(data))

示例:https://wallpaper-mania.com/wp-content/uploads/2018/09/High_resolution_wallpaper_background_ID_77700030695.jpg

这种差异是如何产生的?我怎样才能改进代码以下载图片的方式,它们更小?

f.write(requests.get(url).content)

看起来您是 re-downloading 每个 1024 字节块的整个文件,因此您获得了 222 个图像副本。做到这一点:

f.write(data)