2015-10-26 94 views
0

我在終端中使用wget下載大量圖像列表。使用完整的URL作爲保存的文件名與wget

例子 - $ wget -i images.txt

我在images.txt文件中的所有圖像的URL。

但是,圖像的URL往往是像example.com/unqiueNumber/images/main_250.jpg

,這意味着所有的圖像打印出來命名爲main_250.jpg

我真正需要的是被保存爲圖像的整個網址上的圖片每一個,所以'唯一號碼'是文件名的一部分。

有什麼建議嗎?

+0

'-x'選項將強制創建子目錄以匹配url。例如'wget -x http:// example.com/foo/bar/baz.txt'會將本地文件寫入'./foo/bar/baz.txt' –

+0

謝謝,但我不認爲這會是有用...我需要將圖像命名爲...'example.com-unqiueNumber-images-main_250.jpg' ' – MildTomato

+0

這是超出/超出wget的目的。可能最好的選擇是通過awk/sed類型的東西來運行你的urls文件,並將urls處理成urls +輸出文件規範,然後它是'wget url -O file_to_save_to' –

回答

1

。假定該圖片的網址命名images.txt每行一個URL的文本文件,你可以運行
cat images.txt | sed 'p;s/\//-/g' | sed 'N;s/\n/ -O /' | xargs wget
下載每一個圖像與已形成了URL的文件名。

現在的解釋:

在這個例子中,我將使用

https://www.newton.ac.uk/files/covers/968361.jpg https://www.moooi.com/sites/default/files/styles/large/public/product-images/random_detail.jpg?itok=ErJveZTY

爲images.txt(您可以根據需要添加儘可能多的圖像文件,只要他們在這個相同的格式)。

  • cat images.txt管該文件以標準輸出的內容
  • sed 'p;s/\//-/g'打印與URL到stdout在一行,然後從下一行預期的文件名,像這樣的文件:

    https://www.newton.ac.uk/files/covers/968361.jpg https:--www.newton.ac.uk-files-covers-968361.jpg https://www.moooi.com/sites/default/files/styles/large/public/product-images/random_detail.jpg?itok=ErJveZTY https:--www.moooi.com-sites-default-files-styles-large-public-product-images-random_detail.jpg?itok=ErJveZTY
  • sed 'N;s/\n/ -O /'將每個圖像的兩行(url和預期的文件名)組合成一行,並在中間添加-O選項(這是爲了知道第二個參數是預期的文件名),結果爲這部分看就像這樣:

    https://www.newton.ac.uk/files/covers/968361.jpg -O https:--www.newton.ac.uk-files-covers-968361.jpg https://www.moooi.com/sites/default/files/styles/large/public/product-images/random_detail.jpg?itok=ErJveZTY -O https:--www.moooi.com-sites-default-files-styles-large-public-product-images-random_detail.jpg?itok=ErJveZTY
  • 最後xargs wget運行wget的每條線作爲選擇,在該示例中endresult是在分別命名爲https:--www.newton.ac.uk-files-covers-968361.jpghttps:--www.moooi.com-sites-default-files-styles-large-public-product-images-random_detail.jpg?itok=ErJveZTY當前目錄的兩個圖像。