1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import json import base64 import requests from lxml import etree from collections import OrderedDict from urllib3 import encode_multipart_formdata
session = requests.Session()
header = { "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundarygcfhLR9P0fj0CgyD", "Origin": "http://zhaopianhuandise.com", "Referer": "http://zhaopianhuandise.com/", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"
}
home_url = "http://zhaopianhuandise.com" upload_url = "http://zhaopianhuandise.com/index/shangcuan.html" content_url = "http://zhaopianhuandise.com/index/faceapi.html"
def base64_img(): with open("1a.jpg", 'rb') as f: image = f.read() return image
def get_token(): req = session.get(home_url, headers=header) html = etree.HTML(req.text) upload_token = html.xpath('//input[@id="upload_token"]/@value')[0] return upload_token
def post_upload(name, img_content): params = OrderedDict( [("upload_token", (get_token())), ("image_name", (name)), ("image_file", (name, img_content)), ] ) m = encode_multipart_formdata(params, boundary='----WebKitFormBoundarygcfhLR9P0fj0CgyD') req = session.post(upload_url, headers=header, data=m[0]) try: return req.json() except: return { "code": 401, "msg": "图片类型错误,默认jpg" }
def get_content(mysize, mycolor, name, img_content): ''' mysize:1(一寸(295*413,jpg格式)) mysize:2(小一寸(260*378,jpg格式)) mysize:3(大一寸(390*567,jpg格式)) mysize:4(二寸(413*579,jpg格式)) mysize:5(小二寸(413*531,jpg格式)) mysize:6(大二寸(413*626,jpg格式)) mysize:21(五寸(1051*1500,jpg格式)) :return: ''' data = post_upload(name, img_content) if data["code"] == 401: return { "code": 401, "msg": "照片类型有误,默认jpg" } params = { "isupload": "1", "picpath": data["data"]["picPath"], "mysize": mysize, "code": data["data"]["code"], "mycolor": mycolor } req = session.get(content_url, headers=header, params=params) photo_one = home_url + req.json()["data"]["pic_one"] photo_all = home_url + req.json()["data"]["pic_all"] return { "code": 200, "photo_one": photo_one, "photo_all": photo_all }
|