我们通过抓包工具,抓到上传文件的时候下面的post传输的数据,就是浏览器控制器中显示的Request Payload
------WebKitFormBoundarynZb9BK3DBujba6Z2
Content-Disposition: form-data; name="__VIEWSTATE"
/wEPDwUJNDc2MzM2OTk3ZGS0sDY2lwhw3cK6BpS7DP4LL/pVS1fwACEm67roAq7xaw==
------WebKitFormBoundarynZb9BK3DBujba6Z2
Content-Disposition: form-data; name="__VIEWSTATEGENERATOR"
8AA0AEB0
------WebKitFormBoundarynZb9BK3DBujba6Z2
Content-Disposition: form-data; name="__EVENTVALIDATION"
/wEdABdwEIM+9ljNRxmpNzgN6IJ6GXe/BpiiCcppl3PRVXGLJvVDL1IC8mSY8LCpQhBijMUZUJ8KdHr4AJMS3YNTToqIEIwp6IVONiIdALVRMBxa5csxmd4JIMyklOPCI7iUcwardrzAsF0DsWlP6tF4+5klq0s3naV7Y7FpPV3sbg/8DQh/VRLTESOi0oG2UGbE0JgkNjNt/bZ7hRmry31cW8qsCyNFZFpqymkaMzNK9YGKf8H4SARlUFnLprbLaPwS8nsOO7Qt+va79Co+MIU8gR/jajFt+6qdHWWO2PdeWThPhfXUX4Z8bTZ2FDr79dS5BVz9hBcwb1RQNKN/AwvztWy6giKpHXcdvIWBTSfk71PbvIUsDfd9PVqStNlR+JBwc8zPAKZTvP5rCl1CiqfLwzopnTq+BNiCqezsolefUoaJj7E5jBBkTeha0O922YbyQuS9cjYLEQ5UlQjhyXmaUwj5PIuWZGgzO17dB322+QNkOgeeYaMSG0L26ZIS/0LwGvKAbddqe0EhiJ3WCcDK5gdS
------WebKitFormBoundarynZb9BK3DBujba6Z2
Content-Disposition: form-data; name="File"; filename="pp.pdf"
Content-Type: application/pdf
------WebKitFormBoundarynZb9BK3DBujba6Z2
Content-Disposition: form-data; name="ctl00$content$ddlLanguage"
2052
------WebKitFormBoundarynZb9BK3DBujba6Z2
Content-Disposition: form-data; name="ctl00$content$cmdSaveAttachment"
图片转文字
------WebKitFormBoundarynZb9BK3DBujba6Z2--
这是我们通过python的requests模块上传文件,如同我们浏览器的multipart/form-data形式上传文件,代码如下。
替换files里面的键值对就可以为你所用了。
#coding:utf-8
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf8')
files={'__VIEWSTATE':(None,'/wEPDwUJNDc2MzM2OTk3ZGS0sDY2lwhw3cK6BpS7DP4LL/pVS1fwACEm67roAq7xaw=='),
'__VIEWSTATEGENERATOR':(None,'8AA0AEB0'),
'__EVENTVALIDATION':(None,'/wEdABdwEIM+9ljNRxmpNzgN6IJ6GXe/BpiiCcppl3PRVXGLJvVDL1IC8mSY8LCpQhBijMUZUJ8KdHr4AJMS3YNTToqIEIwp6IVONiIdALVRMBxa5csxmd4JIMyklOPCI7iUcwardrzAsF0DsWlP6tF4+5klq0s3naV7Y7FpPV3sbg/8DQh/VRLTESOi0oG2UGbE0JgkNjNt/bZ7hRmry31cW8qsCyNFZFpqymkaMzNK9YGKf8H4SARlUFnLprbLaPwS8nsOO7Qt+va79Co+MIU8gR/jajFt+6qdHWWO2PdeWThPhfXUX4Z8bTZ2FDr79dS5BVz9hBcwb1RQNKN/AwvztWy6giKpHXcdvIWBTSfk71PbvIUsDfd9PVqStNlR+JBwc8zPAKZTvP5rCl1CiqfLwzopnTq+BNiCqezsolefUoaJj7E5jBBkTeha0O922YbyQuS9cjYLEQ5UlQjhyXmaUwj5PIuWZGgzO17dB322+QNkOgeeYaMSG0L26ZIS/0LwGvKAbddqe0EhiJ3WCcDK5gdS'),
'File':('pp.pdf',open('C:\Administrator\pp.pdf','rb'),'application/pdf'),
'ctl00$content$ddlLanguage':(None,'2052'),
'ctl00$content$cmdSaveAttachment':(None,'图片转文字')
}
response=requests.post(url,files=files)
html = response.text
print html
在官方网站上,requests模拟一个表单数据的格式如下:
files = {'name': (<filename>, <file object>,<content type>, <per-part headers>)}
这一行模拟出来的post数据为:
Content-Disposition: form-data; name=’name’;filename=<filename>
Content-Type: <content type>
<file object>
--boundary
如果filename 和 content-Type不写,那么响应模拟post的数据就不会有二者。