Skip to main content
 首页 » 编程设计

python之如何使用post请求在python中下载文件

2024年10月24日9lexus

我正在尝试使用 python 从应用程序自动下载导出。 这是我的代码:

export_url = 'https://....' 
payload = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} 
r = requests.post(export_url, data=payload) 
print(r) 

响应为 200 但文件不知何故丢失 怎么了?

编辑:

这是我的全部代码:

import requests 
 
URL = 'homepage_after_login' 
LOGIN_URL = 'loginpage' 
 
session = requests.session() 
 
username = 'uname' 
password = 'pass' 
loginformtype = "value" 
submit = "Sign+in" 
 
login_data = {'username'        : username, 
              'password'        : password, 
              'login-form-type' : loginformtype, 
              'submit'          : submit} 
 
session.post(LOGIN_URL, data=login_data) 
 
req = session.get(URL) 
 
export_url = 'https://....' 
payload = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} 
r = requests.post(export_url, data=payload) 
print(r) 

可能它忽略了登录部分?

请您参考如下方法:

也许你应该看看 documentation.

当您打印r 时,您会得到状态码。 (好吧,不完全是,但打印的数字是状态代码。)

你可能想要使用的是r.text:

text

Content of the response, in unicode.

If Response.encoding is None, encoding will be guessed using chardet.

The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set r.encoding appropriately before accessing this property.

或者r.content:

content

Content of the response, in bytes.