Python关于content-type:multipart/form-data 图片上传

我们的请求头必须包含一个特殊的请求头信息:Content-Type,类型为:multipart/form-data,
而且还要有一个内容分割符 (boundary) 用于分割请求体中的多个post的内容。因为接收方解析和还原文件必须要根据这个boundary

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryiSnhC3Aeyzzhqlyt
#  ----WebKitFormBoundaryiSnhC3Aeyzzhqlyt 这个就是内容分隔符

上传的时候,需要加上一串随机码,把我们要上传的文件信息包含在里面
在Python中上传和生成这个随机码信息,需要用到 requests_toolbelt 这个库。

方法一:

方法一:headers就是"Cookie",“Referer”,“User - Agent”,“Content-Type”,着4个大多数情况下足够了;
方法一:采用了,在后面给headers中加入“Content-Type”属性;

# 这个模块用 pip3 install requests_toolbelt
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests

# 请求地址
url = '......./upload.html'	# 上传图片的url

# 头部
headers = {
		"Cookie": "",	# cookie这里就不写了
		"Referer": "http://zclic.tpri.org.cn/licweb/commonVehicle/addDialog.html?vhtype=1",
		"User - Agent": "Mozilla / 5.0(WindowsNT10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 79.0.3945.88Safari / 537.36",
		}

# 文件路径
file = 'C:/Users/Administrator/Desktop/1.jpg' 

multipart_encoder = MultipartEncoder(
fields={  
		# 这里根据需要进行参数格式设置
       'driveImg': ('1.jpg', open(file, 'rb'), 'image/jpeg'),
       "file_id": "0",
       "attName": "driveImg",
       "type": "4",
   },
)
# 将这个content-type:加入headers里面,否则上传不上去
headers_img['Content-Type'] = multipart_encoder.content_type
html = requests.post(url=url, data=multipart_encoder, headers=headers).content.decode('utf-8','ignore')
print("html :",html)

方法二:

方法二:headers就给全写上了,其实写方法一提到过的4个也足够,写全与不全,看个人喜好~;headers中提前写上了 “Content-Type”
在 MultipartEncoder 中加入了 boundary=’----WebKitFormBoundaryplzZR5EZPH1yDBju’

两种方法最大区别就是,headers中的"Content-Type"怎么添加而已~

# 这个模块用 pip3 install requests_toolbelt
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests

# 请求地址
url = ''

# 请求头
headers = {
"Accept":"application/json, text/javascript, */*; q=0.01",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.9",
"Cache-Control":"no-cache",
"Connection":"keep-alive",
"Content-Length":"101932",
"Content-Type":"multipart/form-data; boundary=----WebKitFormBoundaryplzZR5EZPH1yDBju",
"Cookie":"", # 不写啦
"Host":"zclic.tpri.org.cn",
"Origin":"http://zclic.tpri.org.cn",
"Pragma":"no-cache",
"Referer":"http://zclic.tpri.org.cn/licweb/commonVehicle/addDialog.html?vhtype=1",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
"X-Requested-With":"XMLHttpRequest",
}

# 文件路径
file = 'C:/Users/Administrator/Desktop/1.jpg'

multipart_encoder = MultipartEncoder(
    fields={  
    # 这里根据需要进行参数格式设置
        'driveImg': ('1.jpg', open('file ', 'rb'), 'image/jpeg'),
        "file_id": "0",
        "attName": "driveImg",
        "type": "4",
    },
    boundary='----WebKitFormBoundaryplzZR5EZPH1yDBju'
)
# headers_img['Content-Type'] = multipart_encoder_q1.content_type
html = requests.post(url=url, data=multipart_encoder, headers=headers).content.decode('utf-8','ignore')
print("html :",html)

最后:那个请求头处理太麻烦了,附上这个~

1、先建立txt文档,放刚复制的headers
2、pycharm:Ctrl+R 调出正则匹配工具栏
3、写正则 (.*?):(.*)
		 '$1':'$2',

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值