我正在尝试使用requests
提交表单并将文件发布到站点。
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="hdnScrollPosX"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="hdnScrollPosY"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="prv_m_hdnPostBack"
TRUE
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="__EVENTTARGET"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="__EVENTARGUMENT"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="__LASTFOCUS"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="__VIEWSTATECONTROLLER"
2f5c3e5d-9763-4848-8dcd-39a88e64e4f9
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="__VIEWSTATE"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="rdbUploadMethods"
Simple(HTTP)
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="aaImageAlbumName"
att
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="aaImageHiddenNew"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="fileUpload"; filename=""
Content-Type: application/octet-stream
<Binary File Data Not Shown>
---------------------------7e229e1f710b5a
Content-Disposition: form-data; name="HttpUploader1"; filename="C:\Users\Usr1\Desktop\Report.pdf"
Content-Type: application/pdf
<Binary File Data Not Shown>
---------------------------7e229e1f710b5a
Content-Disposition: form-data; name="HttpUploader2"; filename=""
Content-Type: application/octet-stream
<Binary File Data Not Shown>
---------------------------7e229e1f710b5a
Content-Disposition: form-data; name="HttpUploader3"; filename=""
Content-Type: application/octet-stream
<Binary File Data Not Shown>
---------------------------7e229e1f710b5a
Content-Disposition: form-data; name="HttpUploader4"; filename=""
Content-Type: application/octet-stream
<Binary File Data Not Shown>
---------------------------7e229e1f710b5a
Content-Disposition: form-data; name="HttpUploader5"; filename=""
Content-Type: application/octet-stream
<Binary File Data Not Shown>
---------------------------7e229e1f710b5a
Content-Disposition: form-data; name="btnAcquireFiles"
Acquire File(s)
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="aaImageAlbumNotes"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="txtBlockedExtensions"
exe,bat,com,dll
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="txtImageName"
0
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="AllowMultipleSelection"
1
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="hdnIsChanged"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="hdnFileName"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="m_ExcludeForhdnPostBack"
-----------------------------7e229e1f710b5a
Content-Disposition: form-data; name="hdnConfirmOutOfOfficeID"
-----------------------------7e229e1f710b5a--
我尝试使用以下代码复制POST。我收到200响应,但文件未发布。我不确定如何解决此问题?
s = requests.Session()
item_request_headers = {
"User-Agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
"Accept": "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*",
"Accept-Encoding": "gzip,deflate",
"Accept-Language": "en-US"
}
r = s.get(url, headers=item_request_headers)
soup = BeautifulSoup(r.content, 'lxml')
url_post = soup.find('form', id='Form1').get('action')
viewstatcontroller = soup.select('input[name=__VIEWSTATECONTROLLER]')[0]['value']
prv_m_hdnPostBack = soup.select('input[name=prv_m_hdnPostBack]')[0]['value']
aaAllowMultipleSelection = soup.select('input[name=aaAllowMultipleSelection]')[0]['value']
url2 = 'https://url1.ca/UATApp/'+ url_post
files = {'HttpUploader1': (open("C:\\Users\\Usr1\\Desktop\\Report.pdf", 'rb'), 'application/pdf')}
data = {'aaImageAlbumName': 'LGV Results',
'aaImageAlbumNotes': 'Testing!',
'rdbUploadMethods': 'Simple(HTTP)',
'aaAllowMultipleSelection': aaAllowMultipleSelection,
'prv_m_hdnPostBack': prv_m_hdnPostBack,
'txtImageName': '0',
'txtBlockedExtensions': 'exe,bat,com,dll',
'__VIEWSTATECONTROLLER': viewstatcontroller,
'btnAcquireFiles': 'Acquire File(s)',
'hdnScrollPosX':'',
'hdnScrollPosY':'',
'aaImageHiddenNew': '',
'hdnIsChanged': '',
'hdnFileName': '',
'm_ExcludeForhdnPostBack': '',
'hdnConfirmOutOfOfficeID': '',
'__LASTFOCUS': '',
'__EVENTARGUMENT':''
}
item_request_headers.update({'Referer': r.url})
t = s.post(url2, files=files, cookies=r.cookies, data=data,
headers = item_request_headers)