Python 下载文件Demo

如何使用Python进行文件下载呢?

以下载Markdown库为例: Markdown · PyPI 

这里只做做简单下载,复杂操作需要个性化or ChatGPT。

第一种: 用requests库:

import requests

print("downloading with requests")
url = "https://files.pythonhosted.org/packages/9d/80/cc67bfb7deb973d5ae662ee6454d2dafaa8f7c106feafd0d1572666ebde5/Markdown-3.4.3.tar.gz"
r = requests.get(url)
with open("Markdown-3.4.3.tar.gz", "wb") as code:
    code.write(r.content)
print("下载完成")

第二种:用urllib库:需要注意python3中urllib库与2中的不同!

import urllib.request

print("downloading with requests")
url = "https://files.pythonhosted.org/packages/9a/a1/1352b0e5a3c71a79fa9265726e2217f69df9fd4de0bcb5725cc61f62a5df/Markdown-3.4.3-py3-none-any.whl"
f = urllib.request.urlopen(url)
data = f.read()
with open("Markdown-3.4.3-py3-none-any.whl", "wb") as code:
    code.write(data)
print("下载完成")

简单又方便!

你可能感兴趣的:(Python,python,开发语言)