gpt4 book ai didi

Python下载文件,无需外部库

转载 作者:行者123 更新时间:2023-11-30 23:26:58 25 4
gpt4 key购买 nike

我有一个非常大的Python脚本,我正在使用pyinstaller来创建一个exe文件。我需要下载一个 XML 文件,但希望使 exe 尽可能小,因为它已经变得相当大了。

Python 中有从 URL 获取文件的方法吗?如果没有外部库,我找不到任何东西

最佳答案

您可以使用urllib.urlretrieve()将打开的页面保存到指定路径。

或者,您可以使用 urllib.urlopen() 打开网址然后将读取到的文件以二进制方式写入:

import urllib
urllib.urlretrieve(url, destination_path) # First and short way

with open(destination_path, 'wb') as f: # Equivalent to the first, but longer
f.write(urllib.urlopen(url).read())

关于Python下载文件,无需外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22306568/

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