content-type | multipart/form-data; boundary=----WebKitFormBoundaryWljesE53NvLppUTZ |
multipart/form-data的基础是post请求,通过post请求来实现,multipart/form-data与普通的post的差异在请求头和请求体2个部分
Content-Type,且其值也必须规定为multipart/form-data,同时还需要规定一个内容分割符用于分割请求体中的多个post的内容
----WebKitFormBoundaryWljesE53NvLppUTZ 为内容分隔符
from requests_toolbelt import MultipartEncoder def get_phash(): url0='https://www.aliprice.com/Index/searchImage.html?modal=1' multipart_encoder = MultipartEncoder( fields={'image': ('Sdbf72e33d0044532ab33c2e1c720ccfau.jpg', open('Sdbf72e33d0044532ab33c2e1c720ccfau.jpg','rb'),'image/jpeg')}, boundary='----WebKitFormBoundaryJ2aGzfsg35YqeT7X' ) headers={ 'x-requested-with':'XMLHttpRequest', 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36', 'origin':'https://www.aliprice.com', 'referer':'https://www.aliprice.com', } headers['content-type']=multipart_encoder.content_type print(multipart_encoder.content_type) resp = requests.post(url0,data= multipart_encoder,headers=headers) print(resp.text) print(resp.status_code)
2.
import requests,re url='https://uutool.cn/ocr/' headers={ 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36', 'referer': 'https://uutool.cn/', } res = requests.get(url,headers=headers) datatoken =re.findall('<div class="tool-box" @paste="handlePaste" id="toolBox" data-token="(.*?)">',res.text) url1='https://api.uutool.cn/photo/ocr/' headers1={ 'accept': '*/*', 'origin': 'https://uutool.cn', 'referer': 'https://uutool.cn/', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36', } data={'token':datatoken[0]} fields={'file':('text.jpg',open('text.jpg','rb'),'image/jpeg')} resp = requests.post(url1,data=data,files=fields,headers=headers1) print(resp.text)