python - 使用 WebDav API 和 Python 请求下载 NextCloud 文件
问题描述
我在 NextCloud 15 服务器上有一个降价文件,在MyDirectory/
.
到目前为止,我已经能够列出MyDirectory/
内容,尽管文档与实际工作的内容不太一致。
例如,我正在使用https://cloud.example.com/remote.php/webdav/folder
而不是https://cloud.example.com/remote.php/dav/files/username/folder
文档中所说的。我的(工作)代码:
import requests
import xmltodict
webdav_options = """<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:">
<d:prop xmlns:oc="http://owncloud.org/ns">
<d:getlastmodified/>
<d:getcontenttype/>
<oc:fileid/>
</d:prop>
</d:propfind>"""
r = requests.request('PROPFIND', 'https://my-domain.com/nextcloud/remote.php/webdav/MyDirectory',
auth=('username', 'password'),
data=webdav_options
)
xml_dict = xmltodict.parse(r.text, dict_constructor=dict)
for response in xml_dict['d:multistatus']['d:response']:
filename = unquote(response['d:href']).replace(
'/nextcloud/remote.php/webdav/', '')
print(filename) # Working great
现在我想下载文件。按照此处找到的文档不起作用...尝试这样做只会下载一个 html 页面:
url = f'https://my-domain.com/nextcloud/remote.php/webdav/MyDirectory/{filename}'
my_file = requests.get(url, auth=(usernanme, password))
print(my_file.content) # html content
知道如何进行吗?
解决方案
推荐阅读
- python - 为什么下面的代码会抛出 typeError?
- macros - 如何将变量传递给 Nunjucks 宏中的包含项
- vba - VBA:从现有打开的 Internet Explorer 选项卡复制数据/文本
- laravel - safary iframe laravel 此页面已过期
- c# - 在 Unity 中调用了哪个在运行时创建的 Toggle?
- docker - 打印在 docker 容器内运行的 shell 脚本的输出
- javascript - 使用循环内的回调函数从多个 Google 日历插入更新多个 Firebase 节点
- c - 如何使用 exec() 系统调用返回数字的平方并将其存储到文件中?
- c# - 在 LINQ to SQL 中创建自定义 Queryable
- java - 使用 jdbc 在 Oracle sql 中过滤特定日期范围内的数据