网页采集现在用到最多是工具了,像最受站长欢迎的就是火车头了,但有一些站长喜欢使用网页来自定义采集了,下面一起来看一个php 网页采集入库程序代码
php 网页采集程序总结,最近帮朋友做了个采集程序
以www.xxxx.com/shop_list.php?page=1&province=%B1%B1%BE%A9为例
%B1%B1%BE%A9是gb2312的转码,例如
$aa=”北京”;
$aa = @iconv(“utf-8″, “gb2312″,$aa);
echo $bb=urlencode($aa);
我们通过file_get_contents($url) 抓取网页 当然也可以是curl
function getHtml($url){
$ch2 = curl_init($url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch2);
curl_close($ch2);
return $html;
}
抓取我们想要的页面数据,可以设定从哪个位置到哪个位置的区间,取出中间数据,通过以下方法实现
function findneed($wholestr,$strkey1,$strkey2)
{
$num1 = strpos($wholestr , $strkey1)+strlen($strkey1);
$num2 = strpos($wholestr ,$strkey2);
$needstr =substr($wholestr ,