探讨PHP抓取页面的六种方法

    xiaoxiao2023-03-24  3

    下面简单说一下php抓取页面的几种方法及原理:

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

    file()函数

    file_get_contents()函数

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

    curl方式

    fsockopen()函数 socket模式

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

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

    1. file()函数

    <?php

    //定义url

    $url='http://t.qq.com';

    //fiel函数读取内容数组

    $lines_array=file($url);

    //拆分数组为字符串

    $lines_string=implode('',$lines_array);

    //输出内容,嘿嘿,大家也可以保存在自己的服务器上

    echo $lines_string;

    2. file_get_contents()函数

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

    <?php

    //定义url

    $url='http://t.qq.com';

    //file_get_contents函数远程读取数据

    $lines_string=file_get_contents($url);

    //输出内容,嘿嘿,大家也可以保存在自己的服务器上

    echo htmlspecialchars($lines_string);

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

    <?php

    //定义url

    $url='http://t.qq.com';

    //fopen以二进制方式打开

    $handle=fopen($url,"rb");

    //变量初始化

    $lines_string="";

    //循环读取数据

    do{

    $data=fread($handle,1024);

         if(strlen($data)==0) {

             break;

         }

         $lines_string.=$data;

    }while(true);

    //关闭fopen句柄,释放资源

    fclose($handle);

    //输出内容,嘿嘿,大家也可以保存在自己的服务器上

    echo $lines_string;

    4. curl方式

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

    <?php

    // 创建一个新curl资源

    $url='http://t.qq.com';

    $ch=curl_init();

    $timeout=5;

    // 设置URL和相应的选项

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    // 抓取URL

    $lines_string=curl_exec($ch);

    // 关闭cURL资源,并且释放系统资源

    curl_close($ch);

    // 输出内容,嘿嘿,大家也可以保存在自己的服务器上

    echo $lines_string;

    5. fsockopen()函数socket模式

    socket模式能否正确执行,也跟服务器的设置有关系,具体可以通过phpinfo查看服务器开启了哪些通信协议。

    <?php

    $fp = fsockopen("t.qq.com", 80, $errno, $errstr, 30);

    if (!$fp) {

         echo "$errstr ($errno)<br />\n";

    } else {

         $out = "GET / HTTP/1.1\r\n";

         $out .= "Host: t.qq.com\r\n";

         $out .= "Connection: Close\r\n\r\n";

         fwrite($fp, $out);

         while (!feof($fp)) {

             echo fgets($fp, 128);

         }

         fclose($fp);

    }

    6. snoopy插件,推荐大家使用

    使用网上非常流行的snoopy来进行采集,这是一个非常强大的采集插件,并且它的使用非常方便,你也可以在里面设置agent来模拟浏览器信息。

    <?php

    //引入snoopy的类文件

    require('Snoopy.class.php');

    //初始化snoopy类

    $snoopy = new Snoopy;

    $url = "http://t.qq.com";

    //开始采集内容

    $snoopy->fetch($url);

    //保存采集内容到$lines_string

    $lines_string = $snoopy->results;

    //输出内容,嘿嘿,大家也可以保存在自己的服务器上

    echo $lines_string;

    说明:设置agent是在Snoopy.class.php文件的第45行,请在该文件中搜索var $agent。浏览器内容你可以使用PHP来获得,使用echo $_SERVER['HTTP_USER_AGENT']; 可以得到浏览器信息,将echo出来的内容复制到agent里面就可以了。

    转载请注明原文地址: https://ju.6miu.com/read-1202813.html
    最新回复(0)