从码云下载子目录python 工具

安装依赖

pip install requests bs4

 

代码如下:

#!/usr/bin/env python
# -*- coding: utf8 -*-
# 从 gitee 下载子目录
# 用法: python gitee.py <url>  [ 保存目录 ] 如果不提供保存目录,则保存在当前目录下的子目录名中
# 例如: python gitee.py https://gitee.com/zbseaog/php-amqp/tree/master/tests  ~/amqp
# 例如: python gitee.py https://gitee.com/zbseaog/php-amqp/tree/master/tests  # 相当于保存在 ./tests 目录

import requests
import sys
import os
from bs4 import BeautifulSoup

url = sys.argv[1]

if len(sys.argv) == 3:
    path = sys.argv[2]
else:
    path = url.split("/")[-1]

path = path.rstrip('/');


if url.isspace():
    print('url 地址为空!')
    exit()

if not path.isspace():
    if not os.path.exists(path):
        print('新建目录:' + path)
        os.mkdir(path)

html = requests.request('get', url)
html.encoding = 'utf8'
html = html.text

soup = BeautifulSoup(html, 'html.parser')
files = soup.select('div#tree-slider a')
files.pop(0)
files.pop(0)

# print(files)

url = url.replace("/tree/", "/raw/").rstrip('/')
for item in files:

    name = os.path.basename(item['href'])
    newfile = path + '/' + name

    print("文件:" + newfile)
    content = requests.request('get', url + '/' + name)
    content.encoding = 'utf8'
    content = content.text

    with open(newfile, 'w', encoding='utf-8') as fp:
        fp.write(content)

 

posted @   心随所遇  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示