• PHP爬虫抓取网页内容 (simple_html_dom.php)


      使用simple_html_dom.php,下载|文档

      因为抓取的只是一个网页,所以比较简单,整个网站的下次再研究,可能用Python来做爬虫会好些。

     1 <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
     2 <?php
     3 include_once 'simplehtmldom/simple_html_dom.php';
     4 //获取html数据转化为对象
     5 $html = file_get_html('http://paopaotv.com/tv-type-id-5-pg-1.html');
     6 //A-Z的字母列表每条数据是在id=letter-focus 的div内class= letter-focus-item的dl标签内,用find方法查找即为 
     7 
     8     foreach($html->find('.txt-list li a') as $element)
     9     $arr[]= $element->innertext . '<br>';
    10 
    11     $fileName='data.txt';//不用事先建好
    12     $arrLen=count($arr);
    13     for($i=0;$i<$arrLen;$i++){
    14     file_put_contents($fileName,$arr[$i],FILE_APPEND|LOCK_EX);
    15     /*FILE_APPEND|LOCK_EX是往后追加数据,如果没有该参数,则只能插入一条数据
    16         但是如果重新启动抓取时,则会将以往抓取过的数据继续存入*/
    17     }
    18     //以上是抓取的数据然后存到data.text里
    19     $content=file_get_contents($fileName);
    20     $cont=explode("<br>",$content);
    21     $contLen=count($cont);
    22     for($i=0;$i<$contLen;$i++) {
    23         unset($cont[2*$i+1]);
    24     }

    先在 http://www.paopaotv.com/tv-type-id-5-pg-1.html 中找到节点,

    1 foreach($html->find('.txt-list li a') as $element)
    2 $arr[]= $element->innertext . '<br>';

    获得节点内的数据

    获得的数据:

    可以看到,每个获取的数据后面都有个<br>***<br>,这时因为 .txt-list li 下面有两个a,所以会得到两个数据

    1 $content=file_get_contents($fileName);
    2     $cont=explode("<br>",$content);
    3     $contLen=count($cont);
    4     for($i=0;$i<$contLen;$i++) {
    5         unset($cont[2*$i+1]);
    6     }

    获取data.text中的数据,通过 explode("<br>",$content) 将<br>前后的数据分成两部分,将$cont用print_r()函数打印出来后,得到

    可以看出,所有不需要的数据都是奇数项,所以用 unset($cont[2*$i+1]); 函数删掉,显示的时候是:

    但是如何将现在的数组的key重新排序,这个我还没不知道怎么弄,试过array_splice,该函数也不能设定只支持删除奇数的内容。

  • 相关阅读:
    Confluence wiki——CentOS6.8搭建详解
    ifconfig不显示网卡eth0
    VMware vSphere克隆虚拟机
    VMware ESXI6.0服务器安装
    虚拟化技术的基本介绍
    HTTP协议详解
    Shell中的case命令
    Linux通过ssh登录其他服务器,不用输入密码
    Linux下/etc/passwd、/etc/shadow、/etc/group文件
    Linux挂载详解
  • 原文地址:https://www.cnblogs.com/jacson/p/4711537.html
Copyright © 2020-2023  润新知