Archive for July 27th, 2007

You are currently browsing the archives of Enabling Technology .

CURL库的一些用法总结(爬站…)

 

1.CURL库的基本步骤:

     a)curl_init();

    b)curl_setopt();

    c)curl_exec();

    d)curl_close();

2.重点解释curl_setopt里面的内容:

CURLOPT_RETURNTRANSFER:把他设置成1后,再执行curl_exec就不会直接输出到页面而是输出到制定的变量

CURLOPT_URL:设置连接,这个不用说了。

CURLOPT_HEADER:是否在输出的时候带着HTTP头。

CURLOPT_COOKIEFILE跟CURLOPT_COOKIEJAR:在这个里面设置好文件的打开句柄就可以将cookie以netscape方式保存下来

例子:

<?php

$cookieFilePath = dirname(__FILE__).”/cookies/cookie-”.getRand().”.txt”;

@unlink($cookieFilePath);

curl_setopt($login, CURLOPT_COOKIEFILE, $cookieFilePath);

curl_setopt($login, CURLOPT_COOKIEJAR, $cookieFilePath);

?>

CURLOPT_HTTPHEADER:在请求的时候,给远程服务器输入的HTTP头,以数组的形式给出:

例子:

<?php

curl_setopt($login,CURLOPT_HTTPHEADER,array(
 ’Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*’,
 ’Referer: http://www.myspace.com/index.cfm?fuseaction=splash’,
 ’Accept-Language: zh-cn’,
 ’Content-Type: application/x-www-form-urlencoded’,
 //’Accept-Encoding: gzip,deflate’,
 ’User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)’,
 ’Host: login.myspace.com’,
 ’Content-Length: 169′,
 ’Connection: Keep-Alive’,
 ’Cache-Control: no-cache’
 ));

?>

CURLOPT_POST与CURLOPT_POSTFIELDS:模拟POST的句子

例子:

<?php

curl_setopt($login,CURLOPT_POST,1);
 curl_setopt($login, CURLOPT_POSTFIELDS, ‘email=’.$username.’&password=’.$password.’&Remember=Remember& ctl00%24Main%24SplashDisplay%24login%24loginbutton.x=43&ctl00%24Main% 24SplashDisplay%24login%24loginbutton.y=12′);//在这个里面要用urlencode来进行编码

?>

3.网页嗅探:

嗅探器是分析网页结构的重要工具,一般使用:

httpwatch professional版本,建议4以上,装上后,会在IE的工具栏中添加嗅探器图标,爬取网页的时候,使用点击此图标就可以嗅探网页了。

还有一个米老鼠头像的httpsniffer,此工具可以作为辅助,可以看到你的程序里面发送的数据是否正确等等。帮助调试之用.

Posted by micas on Jul 27th 2007 | Filed in Enabling-tech | Comments (0)

爬虫爬取内容后的文章分析

用curl或者file_getcontent的远程文件打开方式得到网页的内容后,如何分析入库?一般来说,分为三类。

第一类:偏移量法

原理很简单,跟读文件的方式差不多,将网页当作一个普通文件处理,用curl_exec存入大字符串后,在其中遍历,获得需要的信息。

优点:速度快,简单

缺点:远程文件的改变直接影响到输出结果,灵活程序不够,需要写一大堆的代码

下面的代码是几个取得偏移的辅助函数

<?php
/**
 * 取得从$start到$end的内容
 *
 * @param string $str
 * @param string $start
 * @param string $end
 * @return string
*/
function getValue($str, $start, $end) {
$pos = strpos($str, $start);
if($pos === false) return false;
$a = $pos + strlen($start);
$b = strpos($str, $end, $a);
if($b === false) return false;
return substr($str, $a, $b - $a);
}
/**
 * 以$pos_num为偏移量,取出$start到$end的内容
 *
 * @param string $str
 * @param string $start
 * @param string $end
 * @param int $pos_num
 * @return string
*/
function getPosValue($str, $start, $end,&$pos_num) {
$pos = strpos($str, $start,$pos_num);
if($pos === false) return false;
$a = $pos + strlen($start);
$b = strpos($str, $end, $a);
if($b === false) return false;
$pos_num=$b+strlen($end);
return substr($str, $a, $b - $a);
}
/**
 * 取得从$start到$end的内容,符合条件的最后一个内容
 *
 * @param string $str
 * @param string $start
 * @param string $end
 * @return string
*/
function getrValue($str, $start, $end) {
$pos = strpos($str, $start);
if($pos === false) return false;
$a = $pos + strlen($start);
$b = strrpos($str, $end);
if($b === false) return false;
return substr($str, $a, $b - $a);
}
/**
 * 以标签形式取得$start到$end的内容
 *
 * @param string $str
 * @param string $start
 * @param string $end
 * @return string
*/
function getLayerValue($str,$start,$end)
{
$pos=strpos($str,$start);
if($pos===false) return false;
$a = $pos + strlen($start);
$b = strpos($str, $end, $a);
if($b===false) return false;
$cont=substr($str, $a, $b - $a);
while(substr_count($cont,$start)!=substr_count($cont,$end))
    {
$b=$b+strlen($end);
$b=strpos($str,$end,$b);
$cont=substr($str, $a, $b - $a);
    }
return $cont;
}
/**
 * 以$pos_num为偏移,以标签形式取得$start到$end的内容
 *
 * @param string $str
 * @param string $start
 * @param string $end
 * @param int $pos_num
 * @return string
*/
function getLayerPosValue($str,$start,$end,&$pos_num)
{
$pos=strpos($str,$start,$pos_num);
if($pos===false) return false;
$a = $pos + strlen($start);
$b = strpos($str, $end, $a);
if($b===false) return false;
$cont=substr($str, $a, $b - $a);
while(substr_count($cont,$start)!=substr_count($cont,$end))
    {
$b=$b+strlen($end);
$b=strpos($str,$end,$b);
$cont=substr($str, $a, $b - $a);
    }
$pos_num=$b+strlen($end);
return $cont;
}
/**
 * 取得最后是$end的内容
 *
 * @param string $str
 * @param string $end
 * @return string
*/
function getValue1($str, $end) {
$pos = strpos($str, $end);
if($pos === false) return false;
return substr($str, 0, $pos);
}
/**
 * 取得开头是$start的内容
 *
 * @param string $str
 * @param string $start
 * @return string
*/
function getValue2($str, $start) {
$pos = strpos($str, $start);
if($pos === false) return false;
return substr($str, $pos + strlen($start));
}
/**
 * 以$pos_num为偏移量,取得开头是$start的内容
 * @param string $str
 * @param string $start
 * @param int $pos_num
 * @return string
*/
function getPosValue2($str, $start,&$pos_num) {
$pos = strpos($str, $start,$pos_num);
if($pos === false) return false;
$pos_num+=strlen($start);
return substr($str, $pos + strlen($start));
}
?>

第二类:正则表达式法

正则方法容易理解,用preg_grep函数或者别的方法来取得值,也是最常用的方法

优点:速度较快,代码很少,适应力很不错。

缺点:写正则很麻烦,尤其是适用范围较广的网页正则

第三类:DomTree方式 

Domtree的意思就是,将html页面分析为DOM,然后利用Dom的方式来获取。

优点:适应力好

缺点:对于Dhtml的页面或者全部使用样式表进行处理的页面非常有效,但对传统的table方式就起不了太大作用了。

类库:PHP跟C++跟JAVA都有相应的类库处理,一般过程是:

使用Tidy来格式化页面后,再使用HtmlParser库进行读取。

Posted by micas on Jul 27th 2007 | Filed in SEO, Enabling-tech | Comments (0)