QzZq博客网址https://www.cnblogs.com/zhangzhangtabszj/p/14592061.html |
|
1 # -- coding:UTF-8 --<code> 2 import requests 3 import re 4 from io import BytesIO 5 6 file = open("E:/cnf/test.txt") # 打开存放链接的TXT文档 7 num = 0 8 while 1: 9 line = file.readline() # 逐行读链接 10 if not line: 11 break 12 print("正在下载 第 %d cnf...." % num) 13 num += 1 14 image_url = line 15 ima = image_url.replace('\n','') #去除每行的'\n',不然会404 16 try: 17 requests.packages.urllib3.disable_warnings() 18 r = requests.get(ima, verify=False) # 创建响应对象 19 path = re.sub("https://gbd.iti.kit.edu/file/", "E:/cnf/", line) 20 # 通过re模块的搜索和替换功能,生成下载文档的保存地址 21 path = re.sub('\n', '', path + ".cnf.zip") # 删除path末尾的换行符'\n' 22 f = open(path, "wb") 23 f.write(r.content) # 将响应对象的内容写下来 24 f.close() 25 except Exception as e: 26 print('无法下载,%s' % e) 27 continue 28 file.close()
|
|
chenzhen0530的博客网址:https://www.cnblogs.com/chenzhen0530/p/10619668.html |
|
1 #coding=utf-8 2 """ 3 目标:提供一个函数能够从网上下载资源 4 输入: 5 url列表 6 保存路径 7 输出: 8 保存到指定路径中的文件 9 要求: 10 能够实现下载过程,即从0%到100%可视化 11 """ 12 # ===================================================== 13 from six.moves import urllib 14 import os 15 import sys 16 17 18 def download_and_extract(filepath, save_dir): 19 """根据给定的URL地址下载文件 20 21 Parameter: 22 filepath: list 文件的URL路径地址 23 save_dir: str 保存路径 24 Return: 25 None 26 """ 27 for url, index in zip(filepath, range(len(filepath))): 28 filename = url.split('/')[-1] 29 save_path = os.path.join(save_dir, filename) 30 urllib.request.urlretrieve(url, save_path) 31 sys.stdout.write('\r>> Downloading %.1f%%' %
|
|
下载单个文件
1 import requests 2 url = "https://gbd.iti.kit.edu/file/002a0330958a14deb23dcc84b5489e8a/" 3 r = requests.get(url) 4 with open("uri2022/cnf02.xz","wb") as f: 5 f.write(r.content) 缺点:没有保留原有文件名。 |
|
下载单个文件-可以解决大文件的下载
1 import requests 2 file_url = 'https://gbd.iti.kit.edu/file/002a0330958a14deb23dcc84b5489e8a/' 3 r = requests.get(file_url, stream = True) 4 with open("uri2022/cnf01.xz","wb") as f: 5 for chunk in r.iter_content(chunk_size = 1024**2): 6 if chunk: 7 f.write(chunk)
|
|
使用wget下载单个文件
1 import wget 2 url = 'https://gbd.iti.kit.edu/file/002a0330958a14deb23dcc84b5489e8a/' 3 #filename = wget.download(url) 4 filename = wget.download(url, out="uri2022/")
|
|
根据QzZq博客的代码改动得到使用wget批量下载文件
1 # -- coding:UTF-8 --<code> 2 import requests 3 import re 4 import wget 5 6 file = open("track_main_2022.uri") # 打开存放链接的TXT文档 7 num = 0 8 while 1: 9 line = file.readline() # 逐行读链接 10 if not line: 11 break 12 print("正在下载 第 %d cnf...." % num) 13 num += 1 14 image_url = line 15 ima = image_url.replace('\n','') #去除每行的'\n',不然会404 16 try: 17 url = ima 18 filename = wget.download(url, out="uri2022/") 19 except Exception as e: 20 print('无法下载,%s' % e) 21 continue 22 file.close()
|
|
由于上面这个批量下载脚本不能满足要求,只能借鉴chenzhen0530的博客从通用脚本改写。
标签:批量,filepath,url,样例,file,import,line,txt,下载 From: https://www.cnblogs.com/yuweng1689/p/16750214.html