通过shell抓取html数据

本文介绍了一种使用Shell脚本从特定网站抓取并整理数据的方法,通过wget下载网页内容后,利用grep和awk等工具实现了对目标数据的有效提取。

最近看一些网站的时候,发现有些数据很有意思,想把数据截取出来,但是想把数据抽取出来很是困难。因为如下的小方框的数字都是上下两行排列,想要把数据抽取到一行是很难实现的。
斯达 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 "

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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23718752/viewspace-1256976/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值