python下载文件的方式

参考大佬的页面:https://www.cnblogs.com/alex-bn-lee/p/9284625.html

1.wget

import wget, os
# 设置下载路径
os.chdir(r"D:/tmp")
url="https://files.cnblogs.com/files/alex-bn-lee/ESRI_01.zip"
# 获取下载文件名称
filename = wget.detect_filename(url)
# 文件下载
wget.download(url)

2.urllib.request

import urllib.request
url = 'https://files.cnblogs.com/files/alex-bn-lee/ESRI_01.zip' 
# 需要自定义文件名称
urllib.request.urlretrieve(url, "demo.zip")

3.浏览器自动下载

import pynput, webbrowser, time
from pynput.mouse import Button
from pynput.keyboard import Key
mouse = pynput.mouse.Controller()
keyboard = pynput.keyboard.Controller()
 
url = 'https://files.cnblogs.com/files/alex-bn-lee/ESRI_01.zip' 
# 打开网页
webbrowser.open(url)
# 反应时间
time.sleep(2)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
time.sleep(1)
mouse.click(Button.left)
# 通过 Ctrl+w 快捷键删除当前页面
with keyboard.pressed(Key.ctrl):
    keyboard.press('w')
    keyboard.release('w')