利用Wget和Shell Script批次下載資料
Wget 是套即有名的下載軟體,在撰寫這篇文章之前,snese 也曾經利用它來其他的用途,例如做備份[1]。由於一些獲取資料的需求,便寫了一個類似砍站的 Script 來完成 snese 的目的,如果你覺得這些對你可能有些幫助,那就繼續看下去吧!
由於對方網站的架構非常的有條理,資料皆由年份來做分類的依據如下面的例子所示,使得在撰寫 Shell Script 的部份,只需用簡單的迴圈便能完成 snese 的需求,反到是 Wget 的參數還需要再研究、著墨一會。
網址範例:http://www.aaa.bbb.com/ccc/ddd/2006/2006-eee/
而每個資料夾下面,則有 snese 所需要的資料,經過一番研究與測試之後,snese 在 Manual Page 裡找出了幾個在此次 Script 有用的參數給各位做個參考。
Directory Options
-nd
–no-directories
Do not create a hierarchy of directories when retrieving recursively. With this option turned on, all files will get saved to the current directory, without clobbering (if a name shows up more than once, the filenames will get extensions .n).
Recursive Retrieval Options
-r
–recursive
Turn on recursive retrieving.
-l depth
–level=depth
Specify recursion maximum depth level depth. The default maximum depth is 5.
會用這些參數的理由其實很簡單,snese 在這邊主要是希望能擷取每資料夾下面全部的資料,所以需要 –recursive 這個參數,但又怕它下載到其他過多無用的資訊,而 –level=depth 就是其限定範圍的方法。由於 Wget 預設會以網址的形式來建立階層的資料,snese 不想搞得這麼複雜,–no-directories 在這則實現了這個需求。
待釐清了 Wget 的問題之後,只需要簡單的 Script 便能完成任務啦!在這就不特別提 snese 的 Script 是怎麼寫的,畢竟是匆忙之間完成,真的不太好意思拿出來獻醜。
在這邊只是提供一個直覺式的問題解法,snese 相信一定有更棒的解決方案,如果知道的朋友也望請能不吝指教呀!
Recent Comments