gpt4 book ai didi

python - r 中的简单 download.file() 不适用于 requests.get

转载 作者:太空宇宙 更新时间:2023-11-04 04:07:48 25 4
gpt4 key购买 nike

我正在尝试将 R 代码转换为 Python 代码。有一条电流线我遇到了麻烦。 (代码片段 1)。

我已经尝试了所有请求变体,python 代码正在创建一个没有任何内容的空白文件。

Requests、wget、urllib.requests 等等等等

(1)

downloader = download.file(url = 'https://www.equibase.com/premium/eqbLateChangeXMLDownload.cfm',destfile = 'C:/Users/bnewell/Desktop/test.xml",quiet = TRUE)              # DOWNLOADING XML FILE FROM SITE
unfiltered = xmlToList(xmlParse(download_file))

(2)

import requests

URL = 'https://www.equibase.com/premium/eqbLateChangeXMLDownload.cfm'

response = requests.head(URL, allow_redirects=True)

最佳答案

import requests, shutil

URL = 'https://www.equibase.com/premium/eqbLateChangeXMLDownload.cfm'

page = requests.get(URL, stream=True, allow_redirects=True,
headers={'user-agent': 'MyPC'})

with open("File.xml", "wb") as f:
page.raw.decode_content = True

shutil.copyfileobj(page.raw, f)

出于某种我不确定的原因手动添加 user-agent 文件下载头。

我使用 shutil 下载可以用 page.iter_content 替换的原始文件

关于python - r 中的简单 download.file() 不适用于 requests.get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56925175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com