用Python脚本从越来越多的美国下载jpg

2024-05-03 04:15:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从一个url下载JPG文件。你知道吗

https://example.com/ebooks/47820eb8e1eda4a69aba442a/images/1.jpg

这本书有641页,我不想手动下载。 该网站不需要特殊的cookies来访问图像 我试图创建一个脚本保存到一个目录,但它没有工作。你知道吗

import urllib.request
import os
os.chdir("E:\Downloads") #your path
i=1;
while i<615:
try:
    urllib.request.urlretrieve("https://example.com/ebooks/47820eb8e1eda4a69aba442a/images/"+ str(i)+".jpg")
i+=1;

我该怎么做?你知道吗


Tags: 文件httpsimportcomurlos网站example
2条回答

因为您并不特别关心Python,所以可以在批处理文件中使用curl

@echo off
setlocal enableextensions enabledelayedexpansion
set /a "x = 1"
:while1
    if %x% leq 615 (
        curl  insecure  silent https://example.com/ebooks/47822eb8e1eda4a69aba442a/images/%x%.jpg -o E:\downloads\%x%.jpg
        set /a "x = x + 1"
        goto :while1
    )
endlocal

卷曲的线条看起来像

curl  insecure  silent https://example.com/ebooks/47820eb8e1eda4a69aba442a/images/1.jpg -o E:\downloads\1.jpg
curl  insecure  silent https://example.com/ebooks/47820eb8e1eda4a69aba442a/images/2.jpg -o E:\downloads\2.jpg
curl  insecure  silent https://example.com/ebooks/47820eb8e1eda4a69aba442a/images/3.jpg -o E:\downloads\3.jpg
import urllib.request
import os
path = "E:\\Downloads\\" #your path
i=1
while i<615:
    try:
        urllib.request.urlretrieve("https://example.com/ebooks/47820eb8e1eda4a69aba442a/images/"+ str(i) +".jpg", path+ str(i) +".jpg")
    except Exception:
        pass
    i+=1

试试这个。 此外,即使您的代码是在python3中,您的问题也被标记为python2.7(urllib.request请求在2.7中不存在)

相关问题 更多 >