通過shell抓取html資料

jeanron100發表於2014-08-24

最近看一些網站的時候,發現有些資料很有意思,想把資料擷取出來,但是想把資料抽取出來很是困難。因為如下的小方框的數字都是上下兩行排列,想要把資料抽取到一行是很難實現的。
斯達 2:3 斯特羅姆 23:57     0-1 


今天嘗試了一下,可以使用shell指令碼來達到目的。
比如我們使用wget來抽取網頁的資料,然後在這個基礎上進行資料的篩查。

[ora11g@rac1 a]$ wget http://www.kufa88.com/jingcai/hunhe?appType=livescore&matchlistDate=2014-08-15
[1] 28401
[ora11g@rac1 a]$ --2014-08-24 04:27:59--  http://www.kufa88.com/jingcai/hunhe?appType=livescore
Resolving www.kufa88.com... 58.83.226.133
Connecting to www.kufa88.com|58.83.226.133|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1815957 (1.7M) [text/html]
Saving to: 鈥渉unhe?appType=livescore鈥

100%[=============================================================================================================================>] 1,815,957    530K/s   in 3.3s   

2014-08-24 04:28:03 (530 KB/s) - 鈥渉unhe?appType=livescore鈥saved [1815957/1815957]


[1]+  Done                    wget http://www.kufa88.com/jingcai/hunhe?appType=livescore


在分析了網站的標籤之後,我寫了如下的shell指令碼,能夠抽取出對應的資料來。

 grep "" *|awk -F\ '{print $2}'|awk -F\< '{print $1}' > win.lst
 grep "
" *|awk -F\ '{print $2}'|awk -F\< '{print $1}'   >tie.lst
 grep "
" *|awk -F\ '{print $2}'|awk -F\< '{print $1}' > lose.lst
 grep "
" *|awk -F\ '{print $2}'|awk -F\< '{print $1}' > rwin.lst
 grep "
" *|awk -F\ '{print $2}'|awk -F\< '{print $1}'  >rtie.lst
 grep "
" *|awk -F\ '{print $2}'|awk -F\< '{print $1}'  >rlose.lst

cnt=`cat win.lst|wc -l`
for i in {1..$cnt}
do
tmp_win=`sed -n ''$i'p' win.lst`
tmp_tie=`sed -n ''$i'p' tie.lst`
tmp_lose=`sed -n ''$i'p' lose.lst`
tmp_rwin=`sed -n ''$i'p' rwin.lst`
tmp_rtie=`sed -n ''$i'p' rtie.lst`
tmp_rlose=`sed -n ''$i'p' rlose.lst`
echo $tmp_win $tmp_tie $tmp_lose $tmp_rwin $tmp_rtie $tmp_rlose
done


抽取出資料之後,展現類似下面的形式,達到了我們預期的目標。
2.98 3.05 2.17 1.53 3.70 4.90
1.81 3.45 3.55 3.45 3.60 1.78
2.60 3.55 2.19 1.51 4.10 4.50
1.38 3.85 7.25 2.33 3.25 2.55
2.30 2.82 3.00 5.40 3.85 1.46
1.34 4.25 7.00 2.15 3.45 2.68
1.29 4.75 7.20 1.93 3.75 2.92
2.25 3.70 2.45 4.60 4.15 1.49
4.05 3.70 1.65 1.96 3.60 2.95
3.65 3.50 1.77 1.81 3.70 3.26
2.95 3.33 2.06 1.58 3.83 4.25
1.35 4.50 6.20 2.12 3.50 2.70
1.68 3.85 3.72 3.02 3.65 1.91
4.95 3.80 1.52 2.18 3.35 2.70
1.54 3.45 5.40 2.80 3.30 2.13
2.39 3.00 2.70 5.65 3.95 1.43


 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23718752/viewspace-1256976/,如需轉載,請註明出處,否則將追究法律責任。

相關文章