4

我有一个带有 HTML 表单的网站。登录后,它会将我带到一个 start.php 站点,然后将我重定向到一个 overview.php。

我想从该服务器下载文件...当我单击 ZIP 文件的下载链接时,链接后面的地址是:

getimage.php?path="vol/img"&id="4312432"

我该如何处理请求?我试图创建一个会话并使用正确的参数执行 GET-Command ......但答案只是我未登录时会看到的网站。

c = requests.Session()
c.auth =('myusername', 'myPass')
request1 = c.get(myUrlToStart.PHP)
tex = request1.text

with open('data.zip', 'wb') as handle:
    request2 = c.get(urlToGetImage.Php, params=payload2, stream=True)
    print(request2.headers)
    for block in request2.iter_content(1024):
        if not block:
            break

        handle.write(block)
4

2 回答 2

3

您正在做的是具有基本身份验证的请求。这不会填写页面上显示的表单。

如果您知道表单向其发送POST请求的 URL,则可以尝试将表单数据直接发送到该 URL

于 2013-09-25T09:54:39.420 回答
3

有相同需求的可以试试这个。。。

import requests
import bs4

site_url = 'site_url_here'
userid = 'userid'
password = 'password'

file_url = 'getimage.php?path="vol/img"&id="4312432"' 
o_file = 'abc.zip'  

# create session
s = requests.Session()
# GET request. This will generate cookie for you
s.get(site_url)
# login to site.
s.post(site_url, data={'_username': userid, '_password': password})
# Next thing will be to visit URL for file you would like to download.
r = s.get(file_url)

# Download file
with open(o_file, 'wb') as output:
    output.write(r.content)
print(f"requests:: File {o_file} downloaded successfully!")

# Close session once all work done
s.close()
于 2018-11-08T22:21:01.693 回答