把图片保存到本地的第一种方法 :
thisimgurl = "http:"+thisimg # 为网址添加上http协议
thisimgurl = thisimgurl.replace("\\","") 把转义字符替换
# print(thisimgurl) # 每一张图片的url
# 将图片下载到本地 用urlretrieve()把图片保存到本地
imgpath = r"C:\Users\Administrator\Desktop\myproject\img"+str(i)+str(j)+".jpg"
request.urlretrieve(thisimgurl,imgpath)
将图片保存到本地的第二种方法 :
from urllib import request
# 伪装浏览器标识
headers03 = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299"}
request03 = request.Request(thisimgurl,None,headers03)
# 自定义urlopen
response = request.urlopen(request03)
# 定义图片保存路径
path = r"C:\Users\Administrator\Desktop\xxoo\image\img.jpg"
# 把文件写进本地文件中
with open (path,"wb") as f :
f.write(response.read()) # 以二进制的方法写进本地文件中