Maison > php教程 > php手册 > PHP实现抓取页面与代码解析

PHP实现抓取页面与代码解析

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-06-06 19:59:32
original
2173 Les gens l'ont consulté

1. 正则表达式 2. PHP DOMDocument对象 3. 插件(如:) 如果你对以上内容已经很了解,以下内容可以飘过…… PHP抓取页面 1. file()函数 ?php $url='http://t.qq.com'; $lines_array=file($url); $lines_string=implode('',$lines_array); echo htmlspecialch

  1. 正则表达式 2. PHP DOMDocument对象 3. 插件(如:)
  如果你对以上内容已经很了解,以下内容可以飘过……
  PHP抓取页面

  1. file()函数

1

2

3

4

5

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

    $lines_array=file($url);

    $lines_string=implode(''$lines_array);

    echo htmlspecialchars($lines_string);

?>

Copier après la connexion

   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

Copier après la connexion

1

2

3

4

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

  $lines_string=file_get_contents($url);

  echo htmlspecialchars($lines_string);

?>

Copier après la connexion

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

1

2

3

4

5

6

7

8

9

10

11

12

13

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

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

        $lines_string="";

        do{

        $data=fread($handle,1024);

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

            break;

        }

        $lines_string.=$data;

        }while(true);

        fclose($handle);

        echo htmlspecialchars($lines_string);

?>

Copier après la connexion

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

1

2

3

4

5

6

7

8

9

10

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

        $ch=curl_init();

        $timeout=5;

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        $lines_string=curl_exec($ch);

        curl_close($ch);

        echo htmlspecialchars($lines_string);

?>

Copier après la connexion

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

1

2

3

4

5

6

7

8

9

<?php $fp = fsockopen("udp://127.0.0.1", 13, $errno$errstr);

        if (!$fp) {

            echo "ERROR: $errno - $errstr\n";

        }

        else {

            fwrite($fp"\n");

            echo fread($fp, 26); fclose($fp);

        }

?>

Copier après la connexion

 6. 插件网上应该有比较多的插件,snoopy插件是在网上搜到的,有兴趣的可以研究一下。
 PHP解析xml(html)
  1. 正则表达式:

1

2

3

4

5

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

        $lines_string=file_get_contents($url);

        eregi(''$lines_string$title);

        echo htmlspecialchars($title[0]);

?>

Copier après la connexion

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

1

2

3

4

5

6

<?php $url='http://www.136web.cn';

        $html=new DOMDocument();

        $html->loadHTMLFile($url);

        $title=$html->getElementsByTagName('title');

        echo $title->item(0)->nodeValue;

?>

Copier après la connexion

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

1

2

3

4

5

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

        include_once('……/simplehtmldom/simple_html_dom.php');

        $html=file_get_html($url); $title=$html->find('title');

        echo $title[0]->plaintext;

?>

Copier après la connexion


 

Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers numéros
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal