批量注册图片,可以根据需要修改data,base64编码

#coding:utf-8
import base64,requests,os,time
def file_name(path_name):
    """路径中获取每个图片"""
    img_list = []
    for root, dirs, files in os.walk(path_name):#walk函数获取文件路径
        for file in files:
            if os.path.splitext(file)[1] == '.jpg':#获取jpg格式
                img_list.append(os.path.join(root, file))
    return img_list

def base_img(path_name,url_input,password):
    """将图片文件转换成base64"""
    path_list = os.listdir(path_name)
    img_name_list = []
    for picture_name in path_list:
        img_name_list.append(picture_name.split(".")[0])#切片获取图片名
    for i in range(len(file_name(path_name))):
        f = open(file_name(path_name)[i], 'rb')  # 二进制方式打开图文件
        img_base = base64.b64encode(f.read())  # 读取文件内容,转换为base64编码
        img_base_str = str(img_base,"utf-8")
        url = url_input
        data = {"pass": password, "employeeNumber": img_name_list[i],"name":str(i),"registerBase64":img_base_str}#请求
        print(data)
        start = time.time()
        r = requests.post(url=url, json=data, verify=False)
        end = time.time()
        print(end - start)
        print(r.json())
        with open("log.txt", "a") as f:
            f.write(str(data)+"\n")
            f.write(str(r.json())+"\n")
            f.write("分割线——————————————————————————————————————————————————————————————————————————————————————————"+"\n")
        f.close()

if __name__ == '__main__':
    path_name = input("文件夹名称:",)
    url_input = input("域名接口地址:")
    password = input("注册密码:")
    start = time.time()
    base_img(path_name,url_input,password)
    end = time.time()
    print(end - start)
    with open("log.txt","a") as f:
        f.write(str(end)+"\n")
    f.close()

用于图片批量注册的功能,这里是实现读取文件夹后,自动遍历jpg格式图片,可以自己根据修改

接口地址需要输入http://ip:接口:端口 

我这里注册面密码可以无视,这个注册密码是data里的password

并且开启后会在同一路径下生成一个log文件

你可能感兴趣的:(python,爬虫)