我想从 github 存储库获取所有文件(以及子目录和其中的文件)。 我不想使用 git 包,因为这需要我安装 git(在 Windows 上,所以这不是自动的)。
愿意使用 urllib 或其他东西来代替。我使用的是 python 2。
我可以从这里获取单个文件< How to download and write a file from Github using Requests > 与:
filename = getcwd() + '\\somefile.txt'
url = 'https://raw.github.com/kennethreitz/requests/master/README.rst'
r=requests.get(url)
with open(filename,'w+') as f:
f.write(r.content)
如何复制整个存储库?
最佳答案
您可以通过向 https://github.com/user/repo/archive/branch.zip
网址发出请求,将整个 github 存储库下载为 .zip 文件。其中 branch
是您要下载的分支的名称(通常是 master)。
示例:
import os
filename = os.path.join(os.getcwd(), 'repo.zip')
url = 'https://github.com/requests/requests/archive/master.zip'
r = requests.get(url)
with open(filename, 'wb') as f:
f.write(r.content)
您还应该以二进制模式打开该文件,以防万一(使用 wb),因为它保存的是 .zip 文件。
关于Python-Requests:从 git 文件夹中获取所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51529060/