首页 > 其他分享 >批量样例文件下载

批量样例文件下载

时间:2022-10-03 11:45:36浏览次数:53  
标签:批量 filepath url 样例 file import line txt 下载

 

 

 

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%%' % 
(float(index + 1) / float(len(filepath)) * 100.0)) 32 sys.stdout.flush() 33 print('\nSuccessfully downloaded') 34 35 36 def _get_file_urls(file_url_txt): 37 """根据URL路径txt文件,获取URL地址列表 38 39 Parameter: 40 file_url_txt: str txt文件本地路径 41 Return: 42 filepath: list URL列表 43 """ 44 filepath = [] 45 file = open(file_url_txt, 'r') 46 for line in file.readlines(): 47 line = line.strip() 48 filepath.append(line) 49 file.close() 50 return filepath 51 52 53 if __name__ == '__main__': 54 file_url_txt = 'file_url_txt.txt' 55 save_dir = 'save_dir/' 56 filepath = _get_file_urls(file_url_txt) 57 download_and_extract(filepath, save_dir)

 

   

下载单个文件

   
 
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

相关文章