python如何递归下载文件夹下所有文件-创新互联-大悟县灿优商贸网站建设

关于灿优商贸

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

python如何递归下载文件夹下所有文件-创新互联

这篇文章主要介绍python如何递归下载文件夹下所有文件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联公司-专业网站定制、快速模板网站建设、高性价比印台网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式印台网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖印台地区。费用合理售后完善,10年实体公司更值得信赖。

最近想备份网站,但是php下载文件的大小是有大小限制的,而我也懒得装ftp再下载了,就想着暂时弄个二级域名站,然后用python(python3)的requests库直接下载网站根目录下的所有文件以及文件夹。(0-0就是这么任性)

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],"./")</pre><p>至此,就在本地目录完全还原下载原网站的路径和文件了。</p><p>以上是“python如何递归下载文件夹下所有文件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!</p>            <br>
            当前题目:python如何递归下载文件夹下所有文件-创新互联            <br>
            文章链接:<a href="http://www.apyobr.com/article/pcghi.html">http://www.apyobr.com/article/pcghi.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/dgggdco.html">css样式在线设计 css样式大全整理版</a>
                </li><li>
                    <a href="/article/dgggdcs.html">html5表格课表制作 手机如何制作课表表格</a>
                </li><li>
                    <a href="/article/dgggdos.html">css手型样式箭头 css样式实现箭头</a>
                </li><li>
                    <a href="/article/dgggoig.html">ios开发冷启动 苹果冷启动怎么启动</a>
                </li><li>
                    <a href="/article/dgggopp.html">ios开发漫画软件叫什么 ios漫画软件哪个好</a>
                </li>        </ul>
    </div>
</div>
<div class="line"></div>
<!--底部-->
<footer id="5">
    <div class="foot1 container">
        <div class="list">
            <div class="item">
                <a href="javascript:;">
                    <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span>
                    <strong>关注我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>索要报价</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>我要咨询</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>找到我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>投诉建议</strong>
                </a>
            </div>
        </div>
        <div class="tel">
            <dl>
                <tel><a href="tel:18982081108" target="_blank">18982081108</a></tel><br>
                <span>也许您需要专业的服务,欢迎来电咨询</span>
            </dl>
            <dl>
                <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br>
                <span>您的需求,是我们前进的动力</span>
            </dl>
        </div>
    </div>
    <div class="friend">
        <div class="container">
            <span class="tit">友情链接:</span>
            <div class="inner">
                <a href="https://www.cdcxhl.com/h5.html" target="_blank">h5页面制作</a><a href="https://www.cdcxhl.com/" target="_blank">建站</a><a href="https://www.cdcxhl.com/zuyong/" target="_blank">服务器租用</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">抖音代运营</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">机柜租用</a><a href="https://www.cdcxhl.com/quanwang.html" target="_blank">全网营销推广</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">成都营销网站建设</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">手机网站建设</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">营销型网站建设</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">品牌网站建设</a><a href="https://www.cdcxhl.com/hangyead/" target="_blank">一元广告投放</a><a href="https://www.cdcxhl.com/waimao.html" target="_blank">成都外贸网站建设</a><a href="https://www.cdcxhl.com/link/" target="_blank">友情链接购买</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站快速收录</a><a href="https://www.cdcxhl.com/xiyun.html" target="_blank">成都移动机房</a><a href="https://www.cdcxhl.com/hangyead/" target="_blank">广告投放平台</a><a href="https://www.cdcxhl.com/waimao.html" target="_blank">外贸营销网站</a><a href="https://www.cdcxhl.com/google.html" target="_blank">谷歌优化排名</a>            </div>
        </div>
    </div>
    <div class="foot">
        <div class="container">
            <div class="footNav">
                <h3>网站建设</h3>
                <a href="http://m.cdxwcx.com/" target="_blank">成都网站建设</a><a href="http://www.cqcxhl.com/service/education.html" target="_blank">重庆教育培训网站建设</a><a href="http://www.myzitong.cn/" target="_blank">绵阳梓潼网站建设</a>            </div>
            <div class="footNav">
                <h3>服务器托管</h3>
                <a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">成都西云机房</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">成都机柜租用</a><a href="http://www.cdfuwuqi.com/jigui/" target="_blank">成都机柜租用</a>            </div>
            <div class="footNav">
                <h3>网站制作</h3>
                <a href="http://m.cdcxhl.com/" target="_blank">成都网站制作</a><a href="http://www.cdkjz.cn/" target="_blank">成都网站制作</a><a href="http://chengdu.cdxwcx.cn/" target="_blank">成都网站制作</a>            </div>
            <div class="footNav">
                <h3>企业服务</h3>
                <a href="https://www.cdcxhl.com/ruanwen/yingxiao/" target="_blank">软文营销</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站免费收录</a><a href="https://www.cdcxhl.com/service/zzgj.html" target="_blank">备案资质管家服务</a>            </div>
            <div class="fr ecode">
                <div class="fl">
                    <img src="/Public/Home/img/ewm.jpg">
                    <p>关注企业微信</p>
                </div>
                <div class="fr slogan">
                    <p class="icon">
                        <a class="ph" href=""><i class="iconfont"></i></a>
                        <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a>
                    </p>
                    <p>
                        <i>想要找 </i> <a href="https://www.cdcxhl.com/xiaochengx.html">小程序开发</a>、<a href="https://www.cdcxhl.com/app.html">APP开发</a>、
                        <a href="https://www.cdcxhl.com/yingxiao.html">营销型网站建设</a>、<a href="https://www.cdxwcx.com/">网站建设</a>、
                        <i><a href="https://www.cdcxhl.com/wangzhandingzhi.html">网站定制开发</a></i> ,就选<a href="http://www.apyobr.com/">灿优商贸</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="bottom container">
            <p class="fl">
                版权所有:大悟县灿优商贸有限公司
                备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">鄂ICP备2025125667号-8</a>
                服务热线:18982081108
            </p>
            <p class="fr">
                <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:
                <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a>
            </p>
        </div>
    </div>
</footer>
<!--在线咨询-->
<div class="fot">
    <ul>
        <li>
            <a href="#" target="_blank">
                <img src="/Public/Home/img/fot1.png" alt="">
                <p>在线咨询</p>
            </a>
        </li>
        <li>
            <a href="tel:18980820575" target="_blank">
                <img src="/Public/Home/img/fot2.png" alt="">
                <p>拨打电话</p>
            </a>
        </li>
    </ul>
</div>
</body>
</html>
<script>
    $(".con img").each(function(){
        var src = $(this).attr("src");    //获取图片地址
        var str=new RegExp("http");
        var result=str.test(src);
        if(result==false){
            var url = "https://www.cdcxhl.com"+src;    //绝对路径
            $(this).attr("src",url);
        }
    });
    window.onload=function(){
        document.oncontextmenu=function(){
            return false;
        }
    }
</script>