使用 python 从 artifactory 下载 .xml 文件时会下载损坏的 xml

Corrupt xml is getting downloaded when downloading .xml file from artifactory using python

我正在使用以下脚本下载人工目录中存在的所有 xml 文件(子文件夹名称包含各种 .xml 文件)

from artifactory import ArtifactoryPath
import shutil,os,requests

url="https://artifactory.xxx.com/artifactory/foldername/subfoldername/"
logdirectory='C:\subfoldername'
if not os.path.exists(logdirectory):
            os.mkdir(logdirectory)
os.chdir(logdirectory)
xmlfiles = ArtifactoryPath(url,auth=(username, password))
for element in xmlfiles:
    print (element )
    xmlname=str(element).split("/")[-1]
    print(xmlname)
    with requests.get(element , auth=(username, password), stream=True) as r:
         with open(xmlname, 'wb') as f:
         shutil.copyfileobj(r.raw, f)

它正在下载名称正确的 xml 文件,但是当我尝试打开文件时它显示损坏的数据。

PS:我也尝试过以下方法(https://pypi.org/project/artifactory/),但使用我的路径和身份验证代码也会导致相同的损坏 xml。粘贴下面的代码以供快速参考

from artifactory import ArtifactoryPath
path = ArtifactoryPath(
    "http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz")

with path.open() as fd:
    with open("tomcat.tar.gz", "wb") as out:
        out.write(fd.read())

请建议我如何下载 xml 文件而不损坏它们

像这样的东西有效:

for element in xmlfiles:
    print(element)
    xmlname= str(element).split("/")[-1]
    print(xmlname)
    req = requests.get(element , auth=(username, password))
         with open(xmlname, 'wb') as f:
            f.write(req.content)

制作中