2016年4月12日 星期二

使用lynx快速取得網頁的超連結並存成清單,並搭配wget下載URL清單檔案

簡介

常常在製作爬蟲程式時,相當需要快速將單頁網頁上面出現的所有超連結存成清單,以方便後續利用wget程式。

殺手級的下載工具的功能主要有:
  1. 找出指定網頁的所有相關URL連結並存成文件
  2. 使用wget指令下載所有相關URL
  3. 使用GNU parallel平行執行多個執行工作
實際範例

下面以gstreamer官方網站的網址作為舉例:
https://gstreamer.freedesktop.org/data/events/gstreamer-conference/2015/

1)利用如下指令快速輸出URL清單列表:
lynx  -dump https://gstreamer.freedesktop.org/data/events/gstreamer-conference/2015/ |grep http|awk '{print $2}' > urls.txt
2)使用wget指令下載URL清單:
wget -i urls.txt 
3)使用GNU parallel工具平行執行多個工作
關於parallel指令使用方法參考:https://www.gnu.org/software/parallel/parallel_tutorial.html
cat urls.txt | parallel "wget -i {}"

將(1)(2)(3)三個步驟觀念整合後可轉寫成shell將其自動化。



補充(若想要監控目錄的下載狀況,可透過watch指令觀察指定目錄的文件狀態):
watch -d ls

補充(若發生zombie的狀況,可使用下列指令清除zombie的parent process): 
kill $(ps -A -ostat,ppid | awk '/[zZ]/{print $2}')

由於zombie process已經結束,所以無法使用kill指令刪除,可透過終結parent process的方式刪除zombie。(當parent process終結時,zombie將繼承init,並且parent process將會等待init並且清除在process table上面的記錄)

參考來源:http://stackoverflow.com/questions/16944886/how-to-kill-zombie-process

A zombie is already dead, so you cannot kill it. To clean up a zombie, it must be waited on by its parent, so killing the parent should work to eliminate the zombie. (After the parent dies, the zombie will be inherited by init, which will wait on it and clear its entry in the process table.) If your daemon is spawning children that become zombies, you have a bug. Your daemon should notice when its children die and wait on them to determine their exit status.

沒有留言:

張貼留言