在python中使用Wget从github下载二进制文件

Cha*_*mer 3 python wget github

我正在创建一个从 GitHub 存储库获取文件的程序,但raw.githubusercontent.com不适用于二进制文件。如果 .ico 文件不是二进制文件,请告诉我如何下载它们。

\n

我正在使用wget.download(),像这样:

\n
import wget\nurl = \xe2\x80\x9c https://raw.githubusercontent.com/user/repository/branch/file\xe2\x80\x9d\nwget.download(url)\n
Run Code Online (Sandbox Code Playgroud)\n

有什么建议么?

\n

Ale*_*yes 8

如果添加?raw=true到文件末尾,它应该可以工作。

例如:我的主页的.ico无法在浏览器中查看。

# Original Link
https://github.com/hayesall/hayesall.github.io/blob/master/favicon.ico

# (1) Appended with `?raw=true`
https://github.com/hayesall/hayesall.github.io/blob/master/favicon.ico?raw=true

# (2) Going through githubusercontent
https://raw.githubusercontent.com/hayesall/hayesall.github.io/master/favicon.ico
Run Code Online (Sandbox Code Playgroud)

选项 (1) 或 (2) 可以通过以下方式下载wget

# Original Link
https://github.com/hayesall/hayesall.github.io/blob/master/favicon.ico

# (1) Appended with `?raw=true`
https://github.com/hayesall/hayesall.github.io/blob/master/favicon.ico?raw=true

# (2) Going through githubusercontent
https://raw.githubusercontent.com/hayesall/hayesall.github.io/master/favicon.ico
Run Code Online (Sandbox Code Playgroud)

检查下载的文件:

$ file favicon.ico
favicon.ico: MS Windows icon resource - 1 icon, 16x16, 32 bits/pixel
Run Code Online (Sandbox Code Playgroud)

在原子中:

Atom 文本编辑器的屏幕截图,显示顶部的代码和底部窗口中的 ico 图像数据。

版本信息:

$ wget --version
GNU Wget 1.19.4 built on linux-gnu.
$ pip freeze | grep "wget"
wget==3.2
Run Code Online (Sandbox Code Playgroud)

如果这仍然不起作用,可能是有问题wget(最近一次更新是在 2015 年)。这是使用以下替代解决方案requests

import wget
url = "https://github.com/hayesall/hayesall.github.io/blob/master/favicon.ico?raw=true"
wget.download(url)
100% [.......................] 1150 / 1150
Run Code Online (Sandbox Code Playgroud)