[英]Downloading binary files from github using Wget in python
我正在創建一個從 GitHub 存儲庫中獲取文件的程序,但raw.githubusercontent.com
不適用於二進制文件。 如果.ico 文件不是二進制文件,那么請告訴我如何下載這些文件。
我正在使用wget.download()
,如下所示:
import wget
url = “ https://raw.githubusercontent.com/user/repository/branch/file”
wget.download(url)
有什么建議么?
如果將?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
選項 (1) 或 (2) 可以使用wget
下載:
import wget
url = "https://github.com/hayesall/hayesall.github.io/blob/master/favicon.ico?raw=true"
wget.download(url)
100% [.......................] 1150 / 1150
檢查下載的文件:
$ file favicon.ico
favicon.ico: MS Windows icon resource - 1 icon, 16x16, 32 bits/pixel
在原子:
版本信息:
$ wget --version
GNU Wget 1.19.4 built on linux-gnu.
$ pip freeze | grep "wget"
wget==3.2
如果這仍然不起作用,則可能是wget
的問題(其最新更新是在 2015 年)。 這是使用requests
的替代解決方案:
import shutil
import requests
url = "https://raw.githubusercontent.com/hayesall/hayesall.github.io/master/favicon.ico"
req = requests.get(url, stream=True)
assert req.status_code == 200
with open("favicon.ico", "wb") as _fh:
req.raw.decode_content = True
shutil.copyfileobj(req.raw, _fh)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.