成都创新互联网站制作重庆分公司

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://cxhlcq.com/article/pcghi.html">http://cxhlcq.com/article/pcghi.html</a>
        </div>
    </div>
</div>
<div class="other container">
    <h3>其他资讯</h3>
    <ul>
        <li>
                <a href="/article/gdgehs.html">JavaScript函数的柯里化是什么意思</a>
            </li><li>
                <a href="/article/gdgepg.html">计算机二级mysql都考什么东西</a>
            </li><li>
                <a href="/article/gdgejs.html">如何去掉index.php</a>
            </li><li>
                <a href="/article/gdgehh.html">php代码编码转换工具怎么使用</a>
            </li><li>
                <a href="/article/gdgeod.html">使用linux的initdb命令初始化PostgreSQL数据库</a>
            </li>    </ul>
</div>
<div class="footer">
    <div class="foota container">
        <div class="foot_nav fl col-lg-8 col-md-8 col-sm-12 col-xs-12">
            <ul>
                <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
                    <h3>网站制作</h3>
                    <a href="https://www.cdcxhl.com/" target="_blank">成都网站制作</a><a href="http://www.cdxtjz.com/" target="_blank">网站制作</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">手机网站制作设计</a><a href="http://www.cxjianzhan.com/mobile/" target="_blank">wap网站制作</a><a href="http://www.cdxwcx.cn/bj/" target="_blank">网站制作报价</a><a href="http://www.kswcd.com/mobile/" target="_blank">手机网站制作</a>                </li>
                <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
                    <h3>企业服务</h3>
                    <a href="https://www.cdcxhl.com/weihu/chengdu.html" target="_blank">成都网站维护</a><a href="https://www.cdcxhl.com/service/ssl.html" target="_blank">ssl证书</a><a href="https://www.cdcxhl.com/ruanwen/yingxiao/" target="_blank">软文推广</a><a href="https://www.cdcxhl.com/service/dailijizhang.html" target="_blank">代理记账</a><a href="https://www.cdcxhl.com/service/zzgj.html" target="_blank">备案资质管家服务</a><a href="https://www.cdcxhl.com/service/trademark.html" target="_blank">商标注册</a>                </li>
                <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
                    <h3>网站建设</h3>
                    <a href="http://m.cdcxhl.cn/shop/" target="_blank">商城网站建设公司</a><a href="http://www.cdkjz.cn/develop/" target="_blank">电商网站建设</a><a href="http://www.cdkjz.cn/wangzhan/menhu/" target="_blank">成都门户网站建设</a><a href="http://www.cdkjz.cn/wangzhan/waimao/" target="_blank">成都外贸网站建设</a><a href="https://www.cdxwcx.com/city/shifang/" target="_blank">什邡网站建设</a><a href="https://www.cdxwcx.com/city/jiangyou/" target="_blank">江油网站建设</a>                </li>
                <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
                    <h3>服务器托管</h3>
                    <a href="https://www.cdcxhl.com/idc/cqstsx.html" target="_blank">重庆水土双线托管</a><a href="https://www.xwcx.net/tgxq/cdghjf.html" target="_blank">成都光华机房</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">机柜租用</a><a href="https://www.cdcxhl.com/tuoguan/" target="_blank">IDC机房托管</a><a href="https://www.cdcxhl.com/idc/cqst.html" target="_blank">重庆电信水土机房托管</a><a href="https://www.cdxwcx.com/jifang/meishan.html" target="_blank">眉山服务器托管</a>                </li>
            </ul>
        </div>
        <div class="footar fl col-lg-4 col-md-4 col-sm-12 col-xs-12">
            <p>全国免费咨询:</p>
            <b>400-028-6601</b>
            <p>业务咨询:028-86922220 / 13518219792</p>
            <p>节假值班:18980820575 / 13518219792</p>
            <p>联系地址:成都市太升南路288号锦天国际A幢1002号</p>
        </div>
    </div>
    <div class="footb">
        <div class="copy container">
            <div class="fl">Copyright © 成都创新互联科技有限公司重庆分公司  <a href="https://beian.miit.gov.cn/" target="_blank">渝ICP备2021005571号</a></div>
            <!--<div class="fr"><a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:<a href="https://www.cdcxhl.com/" target="_blank">创新互联</a></div>-->
        </div>
    </div>
    <div class="link">
        <div class="container">
            友情链接::
            <a href="https://www.cdcxhl.com/" target="_blank">成都网站建设</a>
            <a href="https://www.cdcxhl.com/city/chongqing.html" target="_blank">重庆网站建设</a>
            <a href="">四川网站建设</a>
            <a href="">重庆建设网站</a>
            <a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">移动服务器托管</a>
            <a href="http://www.cdfuwuqi.com/" target="_blank">成都服务器托管</a>
            <a href="https://www.cdcxhl.cn/" target="_blank">云服务器</a>
            <a href="http://www.cdhuace.com/" target="_blank">广告设计制作</a>
            <a href="https://www.cdcxhl.com/sheji/chongqing.html" target="_blank">重庆网页设计</a>
            <a href="https://www.cdcxhl.com/zuo/chongqing.html" target="_blank">重庆做网站</a>
            <a href="https://www.cdcxhl.com/zhizuo/chongqing.html" target="_blank">重庆网站制作</a>
            <a href="">重庆网站建设</a>
            <a href="">重庆网站公司</a>
            <a href="">渝中网站制作</a>
            <a href="">重庆网站设计</a>
        </div>
    </div>
</div>
<div class="foot">
    <ul class="public-celan">
        <li>
            <a href="https://p.qiao.baidu.com/cps3/chatIndex?siteToken=6ce441ff9e2d6bedbdfc2a4138de449e&speedLogId=162260383240185e3_1622603832401_02407&eid=6256368&reqParam=%7B%22from%22%3A1%2C%22sessionid%22%3A%22-100%22%2C%22siteId%22%3A%2211284691%22%2C%22tid%22%3A%22-1%22%2C%22userId%22%3A%226256368%22%2C%22ttype%22%3A1%2C%22pageId%22%3A0%7D" target="_blank" class="a1 db tc">
                <img src="/Public/Home/img/icon-23.png" alt="" class="db auto">
                <span class="span-txt">在线咨询</span>
            </a>
        </li>
        <li>
            <a href="tel:18980820575" class="a1 db tc">
                <img src="/Public/Home/img/icon-24.png" alt="" class="db auto">
                <span class="span-txt">电话咨询</span>
            </a>
        </li>
        <li>
            <a target="_blank" href="tencent://message/?uin=1683211881&Site=&Menu=yes" class="a1 db tc">
                <img src="/Public/Home/img/icon-25.png" alt="" class="db auto">
                <span class="span-txt">QQ咨询</span>
            </a>
        </li>
        <li>
            <a target="_blank" href="tencent://message/?uin=532337155&Site=&Menu=yes" class="a1 db tc public-yuyue-up">
                <img src="/Public/Home/img/icon-26.png" alt="" class="db auto">
                <span class="span-txt">预约顾问</span>
            </a>
        </li>
    </ul>
</div>
<div class="customer">
    <dl class="icon1">
        <dt>
            <a href="tencent://message/?uin=1683211881&Site=&Menu=yes">
                <i class="iconT"><img src="/Public/Home/img/QQ.png" alt=""></i>
                <p>在线咨询</p>
            </a>
        </dt>
    </dl>
    <dl class="icon2">
        <dt><i><img src="/Public/Home/img/weixin.png" alt=""></i><p>微信咨询</p></dt>
        <dd><img src="/Public/Home/img/ewm.png"></dd>
    </dl>
    <dl class="icon3">
        <dt><i><img src="/Public/Home/img/dianhua.png" alt=""></i><p>电话咨询</p></dt>
        <dd>
            <p>028-86922220(工作日)</p>
            <p>18980820575(7×24)</p>
        </dd>
    </dl>
    <dl class="icon4">
        <dt class="sShow">
            <a href="tencent://message/?uin=244261566&Site=&Menu=yes">
                <i><img src="/Public/Home/img/dengji.png" alt=""></i><p>提交需求</p>
            </a>
        </dt>
    </dl>
    <dl class="icon5">
        <dt class="gotop">
            <a href="#top">
                <i><img src="/Public/Home/img/top.png" alt=""></i><p>返回顶部</p>
            </a>
        </dt>
    </dl>
</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>