欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

curl实现站外采集的方法和技巧

程序员文章站 2023-11-23 19:15:46
选择curl的理由 关于curl与file_get_contents,摘抄一段通俗易懂的对比:file_get_contents其实是一堆内置的文件操作函数的合并版本,比...

选择curl的理由

关于curl与file_get_contents,摘抄一段通俗易懂的对比:
file_get_contents其实是一堆内置的文件操作函数的合并版本,比如file_exists,fopen,fread,fclose,专门提供给懒人用的,而且它主要是用来对付本地文件的,但又是因为懒人的原因,同时加入了对网络文件的支持;
curl是专门用来进行网络交互的库,提供了一堆自定义选项,用来应对不同的环境,稳定性自然要大于file_get_contents。

使用方法

1、开启curl支持

由于php环境安装后默认是没有打开curl支持的,需修改php.ini文件,找到;extension=php_curl.dll,把前面的冒号去掉,重启服务即可;

2、使用curl进行数据抓取

复制代码 代码如下:

// 初始化一个 curl 对象
$curl = curl_init();
// 设置你需要抓取的url
curl_setopt($curl, curlopt_url, 'http://www.cmx8.cn');
// 设置header
curl_setopt($curl, curlopt_header, 1);
// 设置curl 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, curlopt_returntransfer, 1);
// 运行curl,请求网页
$data = curl_exec($curl);
// 关闭url请求
curl_close($curl);

3、通过正则匹配找到关键数据

复制代码 代码如下:

//$data是curl_exec返回的的值,即采集的目标内容
preg_match_all("/<li class=\"item\">(.*?)<\/li>/",$data, $out, preg_set_order);
foreach($out as $key => $value){
    //此处$value是数组,同时记录找到带匹配字符的整句和单独匹配的字符
    echo '匹配到的整句:'.$value[0].'
';
    echo '单独匹配到的:'.$value[1].'
';
}

技巧

1、超时的相关设置

通过curl_setopt($ch, opt) 可以设置一些超时的设置,主要包括:

curlopt_timeout 设置curl允许执行的最长秒数。
curlopt_timeout_ms 设置curl允许执行的最长毫秒数。 (在curl 7.16.2中被加入。从php 5.2.3起可使用。 )
curlopt_connecttimeout 在发起连接前等待的时间,如果设置为0,则无限等待。
curlopt_connecttimeout_ms 尝试连接等待的时间,以毫秒为单位。如果设置为0,则无限等待。 在curl 7.16.2中被加入。从php 5.2.3开始可用。
curlopt_dns_cache_timeout 设置在内存中保存dns信息的时间,默认为120秒。

复制代码 代码如下:

curl_setopt($ch, curlopt_timeout, 60);   //只需要设置一个秒的数量就可以
curl_setopt($ch, curlopt_nosignal, 1);    //注意,毫秒超时一定要设置这个
curl_setopt($ch, curlopt_timeout_ms, 200);  //超时毫秒,curl 7.16.2中被加入。从php 5.2.3起可使用

2、通过post提交数据,保留cookie

复制代码 代码如下:

//以下摘抄一个例子过来,用于学习借鉴:
//curl 模拟登录 discuz 程序,适合dz7.0

!extension_loaded('curl') && die('the curl extension is not loaded.');   

$discuz_url = 'http://www.lxvoip.com';//论坛地址   
$login_url = $discuz_url .'/logging.php?action=login';//登录页地址   
$get_url = $discuz_url .'/my.php?item=threads'; //我的帖子   

$post_fields = array();   
//以下两项不需要修改   
$post_fields['loginfield'] = 'username';   
$post_fields['loginsubmit'] = 'true';   
//用户名和密码,必须填写   
$post_fields['username'] = 'lxvoip';   
$post_fields['password'] = '88888888';   
//安全提问   
$post_fields['questionid'] = 0;   
$post_fields['answer'] = '';   
//@todo验证码   
$post_fields['seccodeverify'] = '';   

//获取表单formhash   
$ch = curl_init($login_url);   
curl_setopt($ch, curlopt_header, 0);   
curl_setopt($ch, curlopt_returntransfer, 1);   
$contents = curl_exec($ch);   
curl_close($ch);   
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);   
if(!empty($matches)) {   
    $formhash = $matches[1];   
} else {   
    die('not found the forumhash.');   
}   

//post数据,获取cookie   
$cookie_file = dirname(__file__) . '/cookie.txt';   
//$cookie_file = tempnam('/tmp');   
$ch = curl_init($login_url);   
curl_setopt($ch, curlopt_header, 0);   
curl_setopt($ch, curlopt_returntransfer, 1);   
curl_setopt($ch, curlopt_post, 1);   
curl_setopt($ch, curlopt_postfields, $post_fields);   
curl_setopt($ch, curlopt_cookiejar, $cookie_file);   
curl_exec($ch);   
curl_close($ch);   

//带着上面得到的cookie获取需要登录后才能查看的页面内容   
$ch = curl_init($get_url);   
curl_setopt($ch, curlopt_header, 0);   
curl_setopt($ch, curlopt_returntransfer, 0);   
curl_setopt($ch, curlopt_cookiefile, $cookie_file);   
$contents = curl_exec($ch);   
curl_close($ch);   

var_dump($contents);