下载图片脚本

用于下载网络图片

import requests
import os 
#import img2pdf

img_url_path = 'http://xxx/000%s'#图片url链接文件,需自行创建(每行一个URl)
cookie = 'XXX'
fileName="XXX"

img_save_path = "./%s/" % fileName #文件保存路径(可以自己改,默认保存到脚本的同级目录)
if not os.path.exists(img_save_path):
    os.mkdir(img_save_path)

def from_photo_to_pdf(photo_path,fileName):
    # 1、生成地址列表
    photo_list = os.listdir(photo_path)
    photo_list = [os.path.join(photo_path,i) for i in photo_list]

    # 1、指定pdf的单页的宽和高
    # A4纸张
    # a4inpt = (img2pdf.mm_to_pt(210), img2pdf.mm_to_pt(297))
    # 我的自定义:
    a4inpt = (img2pdf.mm_to_pt(720), img2pdf.mm_to_pt(1080))
    layout_fun = img2pdf.get_layout_fun(a4inpt)
    with open(photo_path+"\\%s.pdf" % fileName, 'wb') as f:
        f.write(img2pdf.convert(photo_list, layout_fun=layout_fun))

for i in range(1,2):
    picname="%03d" % i
    down_url=img_url_path % picname
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
        #'Cookie': cookie
    }
    req = requests.get(url=down_url, headers=header)
    if(req.status_code == 404 or len(req.content) == 17663): #17663用于下载超星阅读电子书终止
       print('下载结束,共有%s' % str(i-1))
       break
    req.encoding = 'utf-8'
    with open('%s%s.jpg' %(img_save_path,picname), "wb") as f:  # 开始写文件,wb代表写二进制文件 图片已数字命名
        f.write(req.content)
    print(picname+'下载完成')

#from_photo_to_pdf(img_save_path,fileName)

上一篇:vue图片放大


下一篇:js图片切换功能