python,下载图片到本地自定文件夹内的方法
比如,我们需要下载下面这张图,图片的网络地址:”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605450278851&di=1e008cc52b92b9ff2e18935197035b67&imgtype=0&src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201311%2F17%2F174124tp3sa6vvckc25oc8.jpg“
把图片下载到D盘tu目录下的test文件夹中
附上全部脚本,从脚本中的注释理解全部过程
from urllib import request import os import random import requests #下载图片脚本用到的库 path2 = r'D://tu' os.mkdir(path2 + './'+"test") #以上两行即在d盘tu目录下创建名称为test的文件夹 c = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605450278851&di=1e008cc52b92b9ff2e18935197035b67&imgtype=0&src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201311%2F17%2F174124tp3sa6vvckc25oc8.jpg" #图片地址 headers = { 'authority': 'cl.bc53.xyz', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'sec-fetch-site': 'none', 'sec-fetch-mode': 'navigate', 'accept-language': 'zh-CN,zh;q=0.9', 'cookie': '__cfduid=d9b8dda581516351a1d9d388362ac222c1603542964', } #头信息,后面请求图片地址的时候需要带上,否则容易禁止访问 print("loading"+" " +c) pic_name = random.randint(0,100)#图片名称随机命令 r = requests.get(c,stream=True,headers=headers)#请求图片地址,注意”r“ with open(path2 + './'+"test" +'./'+str(pic_name) +'.jpg', 'wb') as fd: for chunk in r.iter_content(): fd.write(chunk) #下载脚本,实际就是把图片保存到D盘tu目录test文件夹pic_name文件中
下载完成