用Python 3.5的urllib库批量下载图片

今天接到一个需求,把用户头像下载下来。

用户头像在数据库内是以URL形式保存的,我们先把URL文件跑出来放到a.txt文件内

然后打开Spyder,代码如下:

 from urllib.request import urlopen
addr = open('G:/a.txt')        #导入你的URL文件
i = 0                          #设置起始变量
for url in addr:
    i = i+1                    #一行一行读
    path = str(i)+"me.jpg"     #给图片按先后顺序命名
    data = urllib.request.urlopen(url).read()   #打开URL
    f = open('G:/资料/'+path,"wb")               #读取,写入G盘的资料文件夹
    f.write(data)  
    f.close()
原文地址:https://www.cnblogs.com/helloxia/p/6374099.html