最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python递归下载文件夹下所有文件实例
时间:2019-08-31 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下python递归下载文件夹下所有文件实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1.安装requests库
pip install requests
2.下载文件夹下所有文件及文件夹
这里需要处理的地方主要是文件夹,这里我们判断出该链接是文件夹时,自动创建文件夹,并递归继续进行操作,否则判断该链接是文件,直接使用requests get方法下载,话不多说,看代码
####春江暮客 www.bobobk.com import requests import re import os import sys def help(script): text = 'python3 %s https://www.bobobk.com ./' % script print(text) def get_file(url,path):##文件下载函数 content = requests.get(url) print("write %s in %s" % (url,path)) filew = open(path+url.split("/")[-1],'wb') for chunk in content.iter_content(chunk_size=512 * 1024): if chunk: # filter out keep-alive new chunks filew.write(chunk) filew.close() def get_dir(url,path): #文件夹处理逻辑 content = requests.get(url).text if "Index of" in content: sub_url = re.findall('href="(.*?)" rel="external nofollow" ',content) print(sub_url) for i in sub_url: if "/" in i: i = i.split("/")[0] print(i) if i!="." and i!="..": if not os.direxists(path+i): os.mkdir(path+i) get_dir(url+"/"+i,path+i+"/") print("url:"+url+"/"+i+"nurl_path:"+path+i+"/") else: get_file(url+"/"+i,path) else: get_file(url,path) if __name__ == '__main__': if not sys.argv[1]: help(sys.argv[0]) exit(0) else: get_dir(sys.argv[1],"./")
至此,就在本地目录完全还原下载原网站的路径和文件了。
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27