GitHub目录下载

有时只想下载GIthub上某个大项目下面的一个文件夹,真心麻烦,还要把整个项目clone下来,当然也有使用svn项目检出功能的。本人不喜欢安装太多软件,就用python写了一个脚本来完成。

from bs4 import BeautifulSoup
import requests, os, sys, time


class DownloadDir(object):

    def __init__(self, storDir, repoUrl):
        self.repoUrl = repoUrl
        self.storDir = storDir
        self.sess = requests.Session()

    def run(self):
        self._run(self.repoUrl)


    def _run(self, url):
        response = self.sess.get(url)
        res = self.parseStruct(response.text)
        if isinstance(res, list):
            for r in res:
                newUrl = url + '/' + str(r[0])
                self._run(newUrl)

        else:
            filePath, (fileType, content) = res.popitem()
            self.writeContent(filePath, fileType, content)

        print(url)



    def parseContent(self, html):
        textDict = {}
        self.bs = BeautifulSoup(html, "html.parser")        
        filePath = self.bs.select(".file-navigation .breadcrumb")[0].text.strip()
        content = self.bs.select(".file .data .image")
        if content:
            content = content.select("a")[0].attr("href")
            fileType = 'url'
        else:
            content = self.bs.select(".file .data")[0].text
            fileType = 'text'            

        textDict[filePath] = [fileType, content]
        return textDict

    def parseStruct(self, html):
        struct = []
        self.bs = BeautifulSoup(html, "html.parser")
        if self.bs.select(".file .data"):
            return self.parseContent(html)
        currDir = self.bs.select(".file-navigation .breadcrumb")[0].text.strip()
        res = self.bs.select(".file-wrap .files tr.js-navigation-item")
        for node in res:
            if node == '\n' or not node.select("td.content"):
                continue
            try:
                name = node.select(".content")[0].text.strip()
                isFile = False if node.select(".octicon-file-directory") else True
                struct.append([name, isFile, currDir])
            except:
                continue
        return struct  



    def writeContent(self, filePath, fileType, content):
        filePath = os.path.join(self.storDir, filePath)
        print(filePath)
        mode = 'w'
        if fileType == 'url':
            res = self.sess.get(content)
            content = res.content
            mode = 'wb'

        if not os.path.exists(os.path.dirname(filePath)):
            os.makedirs(os.path.dirname(filePath))

        with open(filePath, mode) as f:
            f.write(content)


if __name__ == "__main__":
    dd = DownloadDir("c:\\","https://github.com/baoboa/pyqt5/tree/master/examples")
    dd.run()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值