首页 > 解决方案 > 使用 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

知道如何进行吗?

标签: pythoncurlpython-requestswebdavnextcloud

解决方案


推荐阅读