日前在 irc.giga.net.tw #linuxtw 版聊天
發現了個機器人還滿有趣的
你只要貼網址,他就會上幫你 parser 出網址的 <title>
 
像我的 59-126-75-42.hinet-ip.hinet.net 就是 歡迎來訓練家的工作室
 
於是,友人 Rickz 就開始搞 shell script,他也寫了個 parser 的功能,用什麼 curl 的指令抓,再抽離
 
我就用 php 實作這個功能~
 
time php -r "\$array=explode('</title',file_get_contents(\$argv[1])); \$array=explode('<title>',\$array[0]);echo \$array[1];" "http://59-126-75-42.hinet-ip.hinet.net"
 
歡迎來到3WA問題解決專家工作室
real	0m0.996s
user	0m0.022s
sys	0m0.006s
 
實際時間為 user+sys
 
我的作法還滿簡單的,只是利用 file_get_contents 去抓網址而以
 
然後…又寫了 python 的寫法
 
time python -c "print __import__('urllib2').build_opener().open(__import__('sys').argv[1]).read().split('<title>')[1].split('</title>')[0]" "http://59-126-75-42.hinet-ip.hinet.net"
 
歡迎來到3WA問題解決專家工作室
real	0m0.916s
user	0m0.053s
sys	0m0.013s
 
似乎不難發現,哪個快,哪個慢了~
 
版上有大大補充了更快的方法,也讓小弟再學一招!
 
time php -r '$str=file_get_contents($argv[1]);if(preg_match("/<title>(.*?)<\/title>/msi", $str, $m ) ) {echo $m[1] ; }' "//59-126-75-42.hinet-ip.hinet.net" 
歡迎來到3WA問題解決專家工作室
real    0m1.008s
user    0m0.023s
sys    0m0.008s