PHP的curl_init采集网页数据 实例教程

01 <?php
02 // 1. 初始化
03 $ch = curl_init();
04  
05 // 2. 设置选项,包括URL
06 curl_setopt($ch, CURLOPT_URL, "http://blog.snsgou.com");
07 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
08 curl_setopt($ch, CURLOPT_HEADER, 0);
09  
10 // 3. 执行并获取HTML文档内容
11 $output = curl_exec($ch);
12  
13 // 4. 释放curl句柄
14 curl_close($ch);
15 ?>

curl_setopt中的 CURLOPT_URL, CURLOPT_RETURNTRANSFER 等参数,请参考php文档手册,里面有详细说明!
现在得到$output内容…使用正则表达式匹配出你需要的内容。

 


 

01 <?php
02 /**
03  * 采集类
04  */
05 class Gather {
06  
07     public $pagestring '';
08     private $db;
09  
10     function __construct() {
11         global $db;
12         $this->db = $db;
13     }
14  
15     function getUrlFile($url) {
16         $url = trim($url);
17         $content '';
18         if (extension_loaded('curl')) {
19             $ch = curl_init();
20             curl_setopt($ch, CURLOPT_URL, $url);
21             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
22             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
23             curl_setopt($ch, CURLOPT_HEADER, 0);
24             $content = curl_exec($ch);
25             curl_close($ch);
26         else {
27             $content file_get_contents($url);
28         }
29         return trim($content);
30     }
31  
32     function get_all_url($code) {
33         preg_match_all('/<a.+?href=["|\']?([^>"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/is'$code$arr);
34         return array('name' => $arr[2], 'url' => $arr[1]);
35     }
36  
37     function get_sub_content($str$start$end) {
38         $start = trim($start);
39         $end = trim($end);
40         if ($start == '' || $end == '') {
41             return $str;
42         }
43         $str explode($start$str);
44         $str explode($end$str[1]);
45         return $str[0];
46     }
47  
48     function vd($var) {
49         echo "<div style=\"border:1px solid #ddd;background:#F7F7F7;padding:5px 10px;\">\r\n";
50         echo "<pre style=\"font-family:Arial,Vrinda;font-size:14px;\">\r\n";
51         var_dump($var);
52         echo "\r\n</pre>\r\n";
53         echo "</div>";
54     }
55  
56 }
57  
58 ?>

 

01 <?php
02 define('ROOT_PATH'str_replace('\\''/', dirname(__FILE__)));
03 //include ROOT_PATH."/Gather.class.php";
04 set_time_limit(0);
05 header("Content-type: text/html; charset=gb2312");
06 //目标网址
07 $url 'http://news.163.com/special/00013C0O/guojibjtj_03.html';
08 //实例化采集机器
09 $gather new Gather();
10 //获取目标网址HTML
11 $html $gather->getUrlFile($url);
12 //定义采集列表区间
13 $start '<div class="bd clearfix">';
14 $end '<div class="pages-1 mt25">';
15 //获取区间内的文章URL和TITLE
16 $code $gather->get_sub_content($html$start$end);
17 $newsAry $gather->get_all_url($code);
18 //打印出结果
19 $gather->vd($newsAry);
20  
21 $tarGetUrl $newsAry['url'][0];
22 //获取目标网址HTML
23 $html $gather->getUrlFile($tarGetUrl);
24 //定义采集列表区间
25 $start '<div id="endText">';
26 $end '<span class="cDGray right" style="white-space:nowrap;">';
27 //获取区间内的文章URL和TITLE
28 $code $gather->get_sub_content($html$start$end);
29 $killHtml '<iframe src="http://g.163.com/r?site=netease&affiliate=news&cat=article&type=tvscreen200x300&location=1" width="200" height="300" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>';
30 $killHtml2 '<a href="http://news.163.com/"><img src="http://img1.cache.netease.com/cnews/img07/end_i.gif" alt="netease" width="12" height="11" border="0" class="icon" /></a>';
31 $code str_replace($killHtml""$code);
32 $code str_replace($killHtml2""$code);
33 $gather->vd($code);
34 ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值