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>
            URL标题:<a href="http://kswsj.cn/article/pcghi.html">http://kswsj.cn/article/pcghi.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/dophgge.html">c语言函数的定义正确的是 c语言函数定义的作用</a>
                </li><li>
                    <a href="/article/dophgio.html">go语言自学方法 go语言怎么学</a>
                </li><li>
                    <a href="/article/dophges.html">关于帝国cms优惠券商城系统的信息</a>
                </li><li>
                    <a href="/article/dophgee.html">帝国cms手游 帝国cms手机端模板</a>
                </li><li>
                    <a href="/article/dophgse.html">linux安装宝塔命令 linux搭建宝塔</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:400-028-6601" target="_blank">400-028-6601</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/quanwang.html" target="_blank">全网整合营销推广</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">分类目录</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布平台</a><a href="https://www.cdcxhl.com/" target="_blank">成都网站制作</a><a href="https://www.cdcxhl.com/" target="_blank">做网站</a><a href="https://www.cdcxhl.com/google.html" target="_blank">谷歌推广公司</a><a href="https://www.cdcxhl.com/weihu/" target="_blank">成都网站维护</a><a href="https://www.cdcxhl.com/" target="_blank">网站制作公司</a><a href="https://www.cdcxhl.com/xiyun.html" target="_blank">移动服务器托管</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">成都微信小程序开发</a><a href="https://www.cdcxhl.com/" target="_blank">建站</a><a href="https://www.cdcxhl.com/waimao.html" target="_blank">外贸网站建设</a><a href="https://www.cdcxhl.com/" target="_blank">成都网站制作公司</a><a href="https://www.cdcxhl.com/gaiban/chengdu.html" target="_blank">成都网站改版</a><a href="https://www.cdcxhl.com/app.html" target="_blank">app软件开发</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">响应式网站建设</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">成都手机网站制作</a><a href="https://www.cdcxhl.com/link/" target="_blank">外链</a>            </div>
        </div>
    </div>
    <div class="foot">
        <div class="container">
            <div class="footNav">
                <h3>网站建设</h3>
                <a href="http://www.cdkjz.cn/" target="_blank">高端网站建设</a><a href="http://m.cdcxhl.cn/qiye/" target="_blank">企业网站建设</a><a href="http://m.cdcxhl.cn/dingzhi/" target="_blank">成都定制网站建设</a>            </div>
            <div class="footNav">
                <h3>服务器托管</h3>
                <a href="https://www.cdcxhl.com/idc/cqhx.html" target="_blank">重庆电信回兴机房托管</a><a href="https://www.cdcxhl.com/idc/ziyang.html" target="_blank">资阳天府云计算中心</a><a href="https://www.xwcx.net/" target="_blank">成都机柜租用</a>            </div>
            <div class="footNav">
                <h3>网站制作</h3>
                <a href="http://www.cxjianzhan.com/" target="_blank">网站制作</a><a href="http://www.mywzjz.com/" target="_blank">绵阳网站制作公司</a><a href="http://chengdu.cdcxhl.com/" target="_blank">成都营销网站制作</a>            </div>
            <div class="footNav">
                <h3>企业服务</h3>
                <a href="https://www.cdcxhl.com/weihu/chengdu.html" target="_blank">成都网站维护</a><a href="https://www.cdcxhl.com/service/service.html" target="_blank">工商服务</a><a href="https://www.cdcxhl.com/ruanwen/" 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="">小程序开发</a>、<a href="">APP开发</a>、
                        <a href="">营销型网站建设</a>、<a href="">网站建设</a>、
                        <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="bottom container">
            <p class="fl">
                版权所有:成都创新互联科技有限公司
                备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a>
                服务热线:028-86922220
            </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="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" 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>