【问题标题】:Is there a way to write python code to download zip file from github?有没有办法编写 python 代码从 github 下载 zip 文件?
【发布时间】:2020-06-27 16:34:52
【问题描述】:

我正在尝试从 github 自动下载数据。假设 github 中的文件位于 zip 文件夹中。有没有办法编写python代码来下载整个zip文件夹?

我使用了请求方法,但它不起作用。代码可以正常运行,但文件明显小于原始文件,我无法通过双击打开它,错误消息文件无效

import requests

URL = 'https://github.com/XYZ/file.zip'
r = requests.get(url)
# open method to open a file on your system and write the contents    
with open("file.zip", "wb") as code:
    code.write(r.content)

【问题讨论】:

  • 我支持@Craicerjack。待证明绝对没有办法,我要说的是:当你遇到困难时,试着报告一下。
  • 请更具体。您是否将 GitHub 项目下载为 zip 文件? GitHub 项目是否有标签/发布/可下载链接? git repo 中是否有版本化的 zip 文件?答案因您遇到的情况而异。
  • 我已经添加了我测试过的代码

标签: python github-api


【解决方案1】:

一种解决方法是在 Python 中运行 shell 命令来克隆文件,然后使用 Python 代码在本地解压缩

from subprocess import check_output

cmd = '!git clone https://github.com/XYI/file.git C:/Users/UserName/Desktop/test'
check_output(cmd, shell=True).decode()

【讨论】:

    猜你喜欢
    • 2014-04-02
    • 1970-01-01
    • 2019-02-11
    • 2023-02-07
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2021-03-08
    • 2016-11-11
    相关资源
    最近更新 更多