使用Python从GitHub存储库下载文件。

huangapple go评论95阅读模式
英文:

Download a file from a GitHub repository using Python

问题

我想从GitHub存储库中下载一个单个文件。在bash中,您可以执行类似以下的操作:

curl -kLSs "https://github.com/mikolalysenko/lena/archive/master.tar.gz" | tar xz --wildcards '*lena.png' --strip-components=1

以在当前工作目录中下载并保存此文件。如何在Python中做到这一点(即不调用bash命令)?

英文:

I would like to download a single file from a GitHub repository. In bash, you could do something like this:

curl -kLSs "https://github.com/mikolalysenko/lena/archive/master.tar.gz" | tar xz --wildcards '*lena.png' --strip-components=1`

to download and save this file in the current working directory. How would one do this using only Python (aka. not calling a bash command)?

答案1

得分: 2

这是要翻译的内容:

"Possible duplicate of https://stackoverflow.com/questions/14120502/how-to-download-and-write-a-file-from-github-using-requests

But if you want to avoid using external modules, the following code can work:

import urllib.request

url = ""
file_name = "file_name"

with urllib.request.urlopen(url) as file, open(file_name, 'w') as f:
    f.write(file.read().decode())

You can change open function parameters to save the file in your desired place"

英文:

Possible duplicate of https://stackoverflow.com/questions/14120502/how-to-download-and-write-a-file-from-github-using-requests

But if you want to avoid using external modules, the following code can work:

import urllib.request

url = ""
file_name = "file_name"

with urllib.request.urlopen(url) as file, open(file_name, 'w') as f:
    f.write(file.read().decode())

You can change open function parameters to save the file in your desired place

答案2

得分: 1

I am not sure if with "pure python" you mean without modules, if not using the method urlretrive from the urllib module could be a solution:

import urllib.request


def main():
    urllib.request.urlretrieve("https://raw.githubusercontent.com/mikolalysenko/lena/master/lena.png", "test.png")


if __name__ == "__main__":
    main()

To download files from github you have to use the raw.githubusercontent.com domain, because the files of github repositories get stored there, but this answer explains it better.

英文:

I am not sure if with "pure python" you mean without modules, if not using the method urlretrive from the urllib module could be a solution:

import urllib.request


def main():
    urllib.request.urlretrieve("https://raw.githubusercontent.com/mikolalysenko/lena/master/lena.png", "test.png")


if __name__ == "__main__":
    main()

To download files from github you have to use the raw.githubusercontent.com domain, because the files of github repositories get stored there, but this answer explains it better.

答案3

得分: 0

以下是代码部分的翻译:

import requests
from io import BytesIO
from pathlib import Path
from zipfile import ZipFile

file_name = 'lena.png'
timeout = 10
url = 'https://github.com/mikolalysenko/lena/archive/master.zip'

r = requests.get(url, timeout=timeout)
if r.ok:
    print('found:', Path(url).name)
    with ZipFile(BytesIO(r.content)) as f:
        for file in f.namelist():
            if file.endswith(file_name):
                print('found:', file)
                data = f.read(file)
                Path(Path(file).name).write_bytes(data)
else:
    print(r)
    print(r.text)

请注意,代码中的注释和字符串没有被翻译。

英文:

I use this do download the zip embedded changlog.txt file from a project I follow on github.

import requests
from io import BytesIO
from pathlib import Path
from zipfile import ZipFile


file_name = 'lena.png'
timeout = 10
url = 'https://github.com/mikolalysenko/lena/archive/master.zip'

r = requests.get(url, timeout=timeout)
if r.ok:
	print('found:', Path(url).name)
	with ZipFile(BytesIO(r.content)) as f:
		for file in f.namelist():
			if file.endswith(file_name):
				print('found:', file)
				data = f.read(file)
				Path(Path(file).name).write_bytes(data)
else:
	print(r)
	print(r.text)

huangapple
  • 本文由 发表于 2023年6月26日 02:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定