php正则采集小例子

这是一个简单的正则匹配采集小程序,虽然简单,但是采集效率还是比较高的,大家可以去扩展一下,补充出一个完整的采集类程序。下面贴代码,大家可以根据自己配置去建库建表。

<?php
set_time_limit ( 0 );
$con=mysql_connect("localhost","root","");
mysql_select_db("zhuanqian");
mysql_query("set names 'utf8'");
$id=0;
while($id<=67600)
{
$data=file_get_contents("http://www.chemicalbook.com/ShowSupplierProductsList6187/%22.$id.%22.htm");

$pattern="/<tr>\s*<td align=left width=46%><a href=\"(.*)\" class=blue onclick=\"blur\(\)\">(.*)<\/a><\/td>\s*<td align=left width=32%>(.*)<\/td>\s*<td align=left width=16%>(.*)<\/td>\s*<td align=left width=6% style=\"border-right:solid 2px #EEEEEE;\"><a target=\"_blank\" href=\"(.*)\" class=blue onclick=\"blur\(\)\">(.*)<\/a><\/td>\s*<\/tr>/iUs";

preg_match_all($pattern, $data, $rs);

for($i=0;$i<count($rs[2]);$i++)
{
$sql="insert into test (eng,chi,cas) values ('".$rs[2][$i]."','".$rs[3][$i]."','".$rs[4][$i]."')";
mysql_query($sql);

}

$id=$id+100;
}

posted @ 2013-05-04 15:26  淘小淘  阅读(335)  评论(0编辑  收藏  举报