GitHub目录下载
来源:互联网 发布:php 单双引号转义 编辑:程序博客网 时间:2024/06/05 10:42
有时只想下载GIthub上某个大项目下面的一个文件夹,真心麻烦,还要把整个项目clone下来,当然也有使用svn项目检出功能的。本人不喜欢安装太多软件,就用python写了一个脚本来完成。
from bs4 import BeautifulSoupimport requests, os, sys, timeclass 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 0
- Github下载某一目录
- GitHub目录下载
- 博客目录及最新Github下载地址
- 用svn下载github中指定目录的文件
- 【Others】从GitHub上下载单个目录或者文件
- 如何在github上下载某个项目的单独某个目录
- GitHub之GitHub下载安装
- Android Github 资源目录
- Github前端star目录
- GitHub 教程资源目录
- GitHub项目下载链接
- Github下载Android源码
- Github下载Android源码
- 使用github下载资源
- github下载工程
- Eclipse下载GitHub源码
- github上传与下载
- 下载ABIDE,GITHUB
- HaoSQL,好用的SQL等数据库一键包发布!
- 关于android屏幕旋转问题
- information_schema系统表——CHARACTER_SETS
- Kerberos报错:kinit: Password incorrect while getting initial credentials
- 每日一练23
- GitHub目录下载
- HaoSQL_好用的SQL等数据库一键包发布!
- Python-day16
- 外观模式
- [02-03]如何保证代码质量
- echarts的使用
- CListCtrl自绘
- 移动硬盘,如何分区使得Mac和Win同时使用?
- Linux iostat命令--监控硬盘性能