Python implements batch downloading of images from excel list display image URLs

  I encountered a requirement: I was given an excel table with many website images in it, and I wanted to download the images locally. To do it manually, just enter the URL in the browser and save the image as. This article introduces batch implementation using python code

 The first step is to download images from the Internet. This uses urlretrieve . The template is as follows:

from urllib.request import urlretrieve
urlretrieve(web_path, save_path)

Reference: https://blog.csdn.net/qq_28304687/article/details/76551196/

Implementation source code:

from urllib.request import urlretrieve
import xlrd2

#excel表的路径,可绝对路径,可相对路径
file_name = "D:\\Desktop\\工作簿1.xlsx"
file = xlrd2.open_workbook(file_name)
# 输出Excel中表的个数
print(file.nsheets)

# 读取某张表
sheet = file.sheet_by_name("Sheet1")
# 获取表的行数
nrows = sheet.nrows
# 获取表的列数
ncols = sheet.ncols
print("nrows: %d, ncols: %d" % (nrows, ncols))

# 获取第x列的数据(从0开始计数)
col_value = sheet.col_values(8)
print(len(col_value))

# 循环读取execl中的图片url地址
for i in range(len(col_value)):
    img_src = col_value[i]
    data = ''
    for j in range(len(img_src)):   #以/为分割符,获取最后的图片名称(逆序)
        if img_src[len(img_src)-1-j] != '/':
            data += img_src[len(img_src)-1-j]
        else:
            break
    temp = data[::-1]     # 倒置操作
    urlretrieve(img_src, "D:\\Desktop\\pic\\"+temp)   # 从网上下载并保存到本地
print('finish!')


Guess you like

Origin blog.csdn.net/gls_nuaa/article/details/132889475