我們通過抓包工具,抓到上傳文件的時候下面的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的數據就不會有二者。