想用php实现网页抓取,怎么实现啊 想用php实现网页抓取,怎么实现啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 简单的: file_get_contents()复杂且强大的: curl 一:采集类库,snoopy.class.php。下载:(http://www.php100.com/html/download/pl/2013/0111/9090.html)说起做网站对大部分站长来讲,采集是必不可缺的部分。很多站长可能直接使用dedecms之类cms内置的采集功能了,但是有时候网站很小没必要使用cms或者cms的内置采集功能满足不了我们的需求的时候怎么办呢。那就可以使用这个采集类库了。使用方法非常简单。使用演示://加载类库文件include("snoopy.php");//要采集的页面地址$url = "http://www.www.shlongyingjixie.com"; $snoopy = new Snoopy;//去抓取页面$snoopy->fetch($url);//输出抓回页面的html echo $snoopy->results;接下来,用正则表达式把你需要的内容匹配出来。这样采集就大功告成了。简单吧! 如果你要抓取的网站没有任何限制的话 就用 file_get_contents 然后用正则表达式选出你要用的内容否则 你就要用curl抓取了 (可以使用代理,可以使用cookie等等) http://topic.csdn.net/u/20080824/07/0125890f-9a98-4296-ad84-c5c748c17581.html PHP多线程比较难实现,效率也不高,我是用python来实现的,原理都一样,都是通过正规来获取内容的 python那效率也不行,特别是处理中文字符之类的。我感觉还是桌面程序那种速度快很多。 function func_ReadPage($strUrl, $nTimeOut = 0, $nReRead = 2) //读网页内容 $nReRead-2 最多读网页两次{ //$nTimeOut = 0 不设读页面最长时间 //$nTimeOut不为0时, $nReRead才有效, $nReRead = 2默认最多读两次 if($nReRead == 0) return 0; $nReReadTem = $nReRead - 1; if($nTimeOut == 0) { if(!$fp = @fopen($strUrl, "r")) return 0; if(!$strPage = stream_get_contents($fp)) //file_get_contents { fclose($fp); return 0; } fclose($fp); return $strPage; } //当$nTimeOut不为0时 $opts = array('http'=>array('method'=>"GET",'timeout'=>$nTimeOut)); $context = stream_context_create($opts); $strPage = @file_get_contents($strUrl, false, $context); if(strlen($strPage) < 10) return func_ReadPage($strUrl, $nTimeOut, $nReReadTem); return $strPage;}///////////////////////////////////////////////////////echo func_ReadPage("http://www.baidu.com"); php中如何实现动态添加查询项 Warning: mysql_query(): 9 is not a valid MySQL-Link resource in ? 急用!!如何比较数据库中的两个表中 的内容,删除相同的数据,保留不一样的数据 得到当前日期前n天的日期,有没有什么比较好的方法? 两个函数之间变量传递的问题 如何上传文件夹? 我下载了一个php的论坛。我要怎样才能打开啊? 请教 送分了,给我做的在线通讯录打个分吧 explode中的两个斜杠各代表啥 Dw cs6 繫結資料庫成亂碼 coreseek/sphinx 多个查询条件
复杂且强大的: curl
使用演示://加载类库文件include("snoopy.php");//要采集的页面地址$url = "http://www.www.shlongyingjixie.com"; $snoopy = new Snoopy;//去抓取页面$snoopy->fetch($url);//输出抓回页面的html echo $snoopy->results;接下来,用正则表达式把你需要的内容匹配出来。这样采集就大功告成了。简单吧!
否则 你就要用curl抓取了 (可以使用代理,可以使用cookie等等)
python那效率也不行,特别是处理中文字符之类的。
我感觉还是桌面程序那种速度快很多。
function func_ReadPage($strUrl, $nTimeOut = 0, $nReRead = 2) //读网页内容 $nReRead-2 最多读网页两次
{
//$nTimeOut = 0 不设读页面最长时间
//$nTimeOut不为0时, $nReRead才有效, $nReRead = 2默认最多读两次
if($nReRead == 0)
return 0;
$nReReadTem = $nReRead - 1;
if($nTimeOut == 0)
{
if(!$fp = @fopen($strUrl, "r"))
return 0;
if(!$strPage = stream_get_contents($fp)) //file_get_contents
{
fclose($fp);
return 0;
}
fclose($fp);
return $strPage;
}
//当$nTimeOut不为0时
$opts = array('http'=>array('method'=>"GET",'timeout'=>$nTimeOut));
$context = stream_context_create($opts);
$strPage = @file_get_contents($strUrl, false, $context); if(strlen($strPage) < 10)
return func_ReadPage($strUrl, $nTimeOut, $nReReadTem);
return $strPage;
}
///////////////////////////////////////////////////////echo func_ReadPage("http://www.baidu.com");