php抓取页面与代码解析

转载:http://www.20ju.com/content/V190044.htm

    在做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求访问url地址, 然后得到html源代码或者xml数据,得到数据我们不能直接输出,往往需要对内容进行提取,然后再进行格式化,以更加友好的方式显现出来。

下面先简单说一下本文的主要内容:

一、 PHP抓取页面的主要方法:

1. file()函数    

2. file_get_contents()函数  

3. fopen()->fread()->fclose()模式  

4.curl方式  

5. fsockopen()函数 socket模式  

6. 使用插件(如:http://sourceforge.net/projects/snoopy/)

二、PHP解析html或xml代码主要方式:

1. 正则表达式    

2. PHP DOMDocument对象  

3. 插件(如:PHP Simple HTML DOM Parser)

如果你对以上内容已经很了解,以下内容可以飘过……

PHP抓取页面

1. file()函数

1 <?php
2    $url='http://t.qq.com';
3    $lines_array=file($url);
4    $lines_string=implode('',$lines_array);
5    echo htmlspecialchars($lines_string);
6 ?>

2. file_get_contents()函数
使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置 allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。

1 <?php
2    $url='http://t.qq.com';
3    $lines_string=file_get_contents($url);
4    echo htmlspecialchars($lines_string);
5 ?>

3. fopen()->fread()->fclose()模式

 1 <?php
 2    $url='http://t.qq.com';
 3    $handle=fopen($url,"rb");
 4    $lines_string="";
 5    do{
 6          $data=fread($handle,1024);
 7          if(strlen($data)==0){break;}
 8          $lines_string.=$data;
 9    }while(true);
10    fclose($handle);
11    echo htmlspecialchars($lines_string);
12 ?>

4. curl方式
使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分号去掉,而且需 要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩展。

 1 <?php
 2    $url='http://t.qq.com';
 3    $ch=curl_init();
 4    $timeout=5;
 5    curl_setopt($ch, CURLOPT_URL, $url);
 6    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 7    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 8    $lines_string=curl_exec($ch);
 9    curl_close($ch);
10    echo htmlspecialchars($lines_string);
11 ?>

5. fsockopen()函数 socket模式
socket模式能否正确执行,也跟服务器的设置有关系,具体可以通过phpinfo查看服务器开启了哪些通信协议,比如我的本地php socket没开启http,只能使用udp测试一下了。

 1 <?php
 2    $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
 3    if (!$fp) {
 4          echo "ERROR: $errno - $errstr<br />";
 5    } else {
 6          fwrite($fp, "");
 7          echo fread($fp, 26);
 8          fclose($fp);
 9    }
10 ?>

6. 插件
网上应该有比较多的插件,snoopy插件是在网上搜到的,有兴趣的可以研究一下。

PHP解析xml(html)

1. 正则表达式:

1 <?php
2    $url='http://t.qq.com';
3    $lines_string=file_get_contents($url);
4    eregi('<title>(.*)</title>',$lines_string,$title);
5    echo htmlspecialchars($title[0]);
6 ?>

2. PHP DOMDocument()对象
如果远程的html或xml存在语法错误,php在解析dom的时候会报错。

1 <?php
2    $url='http://www.136web.cn';
3    $html=new DOMDocument();
4    $html->loadHTMLFile($url);
5    $title=$html->getElementsByTagName('title');
6    echo $title->item(0)->nodeValue;
7 ?>

3. 插件
本文以PHP Simple HTML DOM Parser为例,进行简单介绍,simple_html_dom的语法类似jQuery,它让php操作dom,就像使用jQuery操作dom一样的简单。

1 <?php
2    $url='http://t.qq.com';
3    include_once('../simplehtmldom/simple_html_dom.php');
4    $html=file_get_html($url);
5    $title=$html->find('title');
6    echo $title[0]->plaintext;
7 ?>

当然中国人是富有创造性的,老外往往会在技术上领先,但中国人往往会在使用上更胜一筹,往往做出一些让老外不敢想的功能,比如php的远程抓取与分 析,本来是为数据的整合提供方便。但国人很喜欢这个,于是乎大量的采集站,它们本身不创造任何有价值的内容,就是靠抓取别人的网站内容,并把它据为己有。 在百度里输入“php小”关键词,suggest列表第一个就是“php小偷程序”,然后把同样的关键词放入google,哥只能笑而不语。

posted @ 2012-07-03 15:07  subsir  阅读(385)  评论(0编辑  收藏  举报