如何使用gallery-dl批量下載圖像

假設你是個追星粉、或者單純迷戀特特上的某個女菩薩、或者是個無限循環播放喬丹視頻的球迷、或者單純爲了學習知識,豐富課外業餘生活。那麼你一定會有批量下載圖片(包含視頻)的需求,別告訴我你是挨個點的。

以前我這種需求不是很強烈,直到有一天我發現某個喜歡的人退網了,可能是被封殺,也有可能是號賣了,或者單純不玩了。

這個時候,總是讓人後悔沒事前多下載點東西保存。

gallery-dl 就是個不錯的圖片下載工具。

且看下面的實踐教程:

1. 安裝

我用的 Arch linux,直接 paru 安裝。

1paru -S gallery-dl

非常簡單直接,別的發行版,或者操作系統,請看官方倉庫 https://github.com/mikf/gallery-dl 說明。

2. 配置示例

以推特爲例

 1➜  ~ cat .config/gallery-dl/config.json
 2{
 3  "extractor": {
 4    "base-directory": "~/Pictures/",
 5    "twitter": {
 6      "videos": true,
 7      "retries": 5,
 8      "sleep": "2.0-3.5",
 9      "sleep-429": 2,
10      "image-filter": "user is author",
11      "filename": "{author[name]}-{tweet_id}-{date:%Y%m%d_%H%M%S}-img{num}.{extension}"
12    }
13  }
14}
  • base-directory 下載後存放的目錄
  • videos 下載示例,只下載圖片就改爲 false
  • retries 重試次數 5
  • sleep 下載間隔 2.0s 至 3.5s,太快了小心被封 ip,老實點爲好
  • sleep-429 遇到 429 錯誤等待 2s
  • image-filter 是 gallery-dl 的圖片屬性過濾規則,通過 Python 表達式篩選需要下載的圖片,可以寫得很複雜。user is author 僅下載目標用戶原創圖片,未測試求證。
  • filename 文件名格式化成什麼樣子,誰也不想下載的圖片命名亂七八糟的吧!

以上這些就包含核心配置,反正我就是這麼寫的。

至於其它平臺,比如 instagram 是什麼樣的寫法,需要看看官方的配置文檔。平臺反爬策略千千萬,配置不一樣完全能理解。

3. 下載測試

示例命令:gallery-dl -vv --cookies-from-browser firefox "https://x.com/zoe2496/media

其實上面的配置裏面少了 cookies 部分,核心作用就是給 gallery-dl 提供有效的 X 賬號登錄憑證,讓 gallery-dl 能“僞裝”成你已登錄的瀏覽器,下載需要授權的圖片視頻等內容。

--cookies-from-browser firefox 參數的含義,gallery-dl 自動從你的 Firefox 瀏覽器中提取已保存的 X 賬號登錄 cookies,無需你手動導出 Cookies 文件再配置。

比如你是 Linux,它就會從 /home/mephisto/.mozilla/firefox/5esrorfn.default-release/cookies.sqlite 這樣類似的路徑篩選出屬於 x.com 域名的 Cookies,只要瀏覽器之前已登錄 X 賬號,Cookies 處於有效期(通常幾天到幾周),這樣還是很貼心的。

我一般 Firefox 找到要下載的賬號,然後後運行下面的命令下載:

 1➜  twitter gallery-dl -vv --cookies-from-browser firefox  "https://x.com/zoe2496/media"
 2[gallery-dl][debug] Version 1.31.1
 3[gallery-dl][debug] Python 3.13.11 - Linux-6.18.2-arch2-1-x86_64-with-glibc2.42
 4[gallery-dl][debug] requests 2.32.5 - urllib3 2.6.2
 5[gallery-dl][debug] Configuration Files ['${HOME}/.config/gallery-dl/config.json']
 6[gallery-dl][debug] Starting DownloadJob for 'https://x.com/zoe2496/media'
 7[twitter][debug] Using TwitterMediaExtractor for 'https://x.com/zoe2496/media'
 8[cookies][debug] Extracting cookies from /home/mephisto/.mozilla/firefox/5esrorfn.default-release/cookies.sqlite
 9[cookies][debug] Only loading cookies not belonging to any container
10[cookies][info] Extracted 1793 cookies from Firefox
11[urllib3.connectionpool][debug] Starting new HTTPS connection (1): x.com:443
12......此處省略......
13[twitter][debug] Sleeping 3.39 seconds (download)
14b[urllib3.connectionpool][debug] Starting new HTTPS connection (1): pbs.twimg.com:443
15[urllib3.connectionpool][debug] https://pbs.twimg.com:443 "GET /media/G9cQxetaYAIlyhR?format=jpg&name=orig HTTP/1.1" 200 921878
16/home/mephisto/Pictures/twitter/zoe2496/zoe2496-2006090615453544936-20251230_195048-img1.jpg
17[twitter][debug] Sleeping 3.26 seconds (download)
18[urllib3.connectionpool][debug] Starting new HTTPS connection (1): video.twimg.com:443
19[urllib3.connectionpool][debug] https://video.twimg.com:443 "GET /amplify_video/2006085163848269824/vid/avc1/1280x720/m-ShyiwCyNr8TJGh.mp4?tag=21 HTTP/1.1" 200 1299406
20/home/mephisto/Pictures/twitter/zoe2496/zoe2496-2006085231040946664-20251230_192924-img1.mp4
21[twitter][debug] Sleeping 2.32 seconds (download)
22[urllib3.connectionpool][debug] https://pbs.twimg.com:443 "GET /media/G9cJXkHawAASbGl?format=jpg&name=orig HTTP/1.1" 200 176456
23/home/mephisto/Pictures/twitter/zoe2496/zoe2496-2006082468605247944-20251230_191826-img1.jpg

看上面的輸出,圖片隨機間隔 2.0-3.5 秒,都下載下來了,文件名也很規範,心裏樂開花啊😄。

4. 其它

上面的會全部下載用戶 media 的圖片,你也可以自己寫過濾器:

  • "image-filter": ["user is author", "width >= 1200", "height >= 800"] // 組合使用:原創 + 高清

我比較懶,精力有限,外網菩薩高手漫天飛,看看就好,我選擇全部下載。

有志青年,要多花時間搞錢學真本領。友情提示,本文純屬技術分享,網絡不是法外之地,低調行事,且行且珍惜。

最後修改於: Wednesday, December 31, 2025

相關文章:

翻譯: