从码云下载子目录python 工具

发布时间 2023-07-12 23:56:06作者: 心随所遇

安装依赖

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)


for item in files:

    name = os.path.basename(item['href'])
    print("文件:" + path + '/' + name)
    content = requests.request('get', 'https://gitee.com' + item['href'])
    content.encoding = 'utf8'
    content = content.text

    with open(path + '/' + name, 'w', encoding='utf-8') as fp:
        fp.write(content)