PythonRequests:从git文件夹获取所有文件

2024-04-24 19:01:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从github repo获取所有文件(以及子目录和其中的文件)。 我不想使用git包,因为这要求我安装git(在windows上,所以这不是自动的事情)。在

愿意使用urllib或其他东西代替。我在python2上。在

我可以从这里<;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)

如何复制整个回购?在


Tags: 文件togitltgithuburlwindowsrepo
1条回答
网友
1楼 · 发布于 2024-04-24 19:01:20

通过向https://github.com/user/repo/archive/branch.zipurl发出请求,可以将整个github repo作为.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文件。在

相关问题 更多 >