编程语言
首页 > 编程语言> > Python 批量下载图片简单代码

Python 批量下载图片简单代码

作者:互联网

简单代码

# 导入函数库
import requests
import os
import time

# 创建目录
goalPath = "D:\\test"
if not os.path.exists(goalPath):
    os.mkdir(goalPath)

# url 变化部分独立
countL = ""
countR = ""
imgFormat = ".jpg"
startIndex = 1
endIndex = 9

for i in range(1, endIndex + 1):
    # 延时
    time.sleep(0.5)
    # 定义网址
    url = 'https://xxxxx' + countL + '/' + str(i) + countR + imgFormat
    r = requests.get(url)
    # 判断文件状态
    status = r.status_code
    if status != 404:
        # 下载头像文件
        with open("D:\\test\\"+ str(i) + imgFormat, "wb") as file:
            file.write(r.content)
            # 输出下载结果
            print ('已下载头像图片:' + str(i))

标签:status,imgFormat,批量,Python,代码,url,goalPath,str,import
来源: https://www.cnblogs.com/Yogile/p/16442602.html