• PHP使用DomDocument抓取HTML内容


    有时候会有需要从一个HTML页面来分离出你需要的一些数据来进行处理。

    当然自己分析文件肯定可以,但是比较快速且方便的是使用正则表达式或者DOM。

    鉴于正则表达式我不熟悉,所以我打算使用DOM来完成。

     

    先谈谈我的需求,我要从一个HTML页面的一个表格中提取数据并且将这个数据整理出来加入到MySQL数据库中。

    假设目标HTML中我感兴趣的Table有3列,分别是ID,Name,内容。

    index.php;

    <?php
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    require_once('ContentManager.php');
    //建立Dom对象,分析HTML文件;
     $htmDoc = new DOMDocument;
     $htmDoc->loadHTMLFile($urlTarget );
     $htmDoc->normalizeDocument();
    //获得到此文档中每一个Table对象;
     $tables_list = $htmDoc->getElementsByTagName('table');
    //测试Table Count;
     $tables_count = $tables_list->length;
     foreach ($tables_list as $table)
     {
     //得到Table对象的class属性
     $tableProp = $table->getAttribute('class');
     if ($tableProp == 'target_table_class')
     {
     $contentMgr = new ContentManager();
     $contentMgr->ParseFromDOMElement($table);
    //这里myParser就完成了分析动作。然后就可以进行需要的操作了。
     //比如写入MySQL。
     $contentMgr->SerializeToDB();
     }
     }
     ?>

     
    ContentManager.php

    <?php
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     * Description of ContentParser
     *
     * @author xxxxx
     */
     require_once('ContentInfo.php');
     class ContentManager {
     //put your code here
     var $ContentList;
     public function __construct() {
     $this->ContentList = new ArrayObject();
     }
    public function ParseFromDOMElement(DOMElement $table)
     {
     $rows_list = $fundsTable->getElementsByTagName('tr');
     $rows_length = $rows_list->length;
     $index = 0;
    foreach ($rows_list as $row)
     {
     $contentInfo = new ContentInfo();
     $contentInfo->ParseFromDOMElement($row);
     $this->ContentList->append ($contentInfo);
     }
    //test how many contents parsed.
     $count = $this->fundsInfoArray->count();
     echo $count;
     }
    public function SerializeToDB()
     {
     //写入数据库,代码略。
     }
     }
    ?>

    contentinfo.php

    <?php
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     * Description of ContentInfo
     *
     * @author xxxxx
     */
    class ContentInfo {
        //put your code here
        var $ID;
        var $Name;
        var $Content;
        public function ParseFromDOMElement(DOMElement $row)
        {
            $cells_list = $row->getElementsByTagName('td');
            $cells_length = $row->length;
     
            $curCellIdx = 0;
            foreach ($cells_list as $cell)
            {
                switch ($curCellIdx++)
                {
                    case 0:
                        $this->ID = $cell->nodeValue;
                        break;
                    case 1:
                        $this->Name = $cell->nodeValue;
                        break;
                    case 2:
                        $this->Content = $cell->nodeValue;
                        break;
                }
            }
        }
    }
     
    ?>

    一点小心得,DOM中每个Element都可以getAttribute取出属性,这些属性可以区分你分析的DOMObject。

    举例来说,比如上述我分析的Target HTML有很多表格,但是我发现目标表格的class属性和其他表格是不一样的。

    所以,这个属性就可以来区分我要分析的是哪个表格。

     

    当然更多DOM的东西,大家可以去参考PHP Manual或者是,用IDE(NetBeans7.0就可以)转到类声明,看类接口。

    有方法使用说明以及参数说明。可以参考参考。

    转载自:http://blog.csdn.net/xyzhaopeng/article/details/6626340

  • 相关阅读:
    尾递归
    Appium环境搭建
    虚拟机与主机的相互访问,虚拟机访问外网
    Python
    npm i 安装
    redis过期键删除策略
    Redis的过期策略和内存淘汰机制
    redis的两种持久化方案
    JVM 方法内联
    进程/线程/协程
  • 原文地址:https://www.cnblogs.com/gaohj/p/3332531.html
Copyright © 2020-2023  润新知