原文链接:http://blog.csdn.net/xyzhaopeng/article/details/6626340

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

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

index.php

<pre class="php" name="code"><?php  

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
$urlTarget = "http://www.xxxx.com/targethtmlpage.html"; 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();
}
}
?>
</pre><br>

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;
}
}
}
} ?>

ZH奶酪:PHP 使用DOMDocument抓取网页的更多相关文章

  1. java抓取网页数据,登录之后抓取数据。

    最近做了一个从网络上抓取数据的一个小程序.主要关于信贷方面,收集的一些黑名单网站,从该网站上抓取到自己系统中. 也找了一些资料,觉得没有一个很好的,全面的例子.因此在这里做个笔记提醒自己. 首先需要一 ...

  2. 使用JAVA抓取网页数据

    一.使用 HttpClient 抓取网页数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  3. Java 抓取网页中的内容【持续更新】

    背景:前几天复习Java的时候看到URL类,当时就想写个小程序试试,迫于考试没有动手,今天写了下,感觉还不错 内容1. 抓取网页中的URL 知识点:Java URL+ 正则表达式 import jav ...

  4. C语言调用curl库抓取网页图片

    思路是先用curl抓取网页源码,然后以关键字寻找出图片网址.   #include <stdio.h> #include <stdlib.h> #include <str ...

  5. [转载]爬虫的自我解剖(抓取网页HtmlUnit)

    网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源HtmlUnit包,4行代码就OK啦,例子如下: 1 2 3 4 final WebClient webClie ...

  6. C语言调用curl库抓取网页图片(转)

    思路是先用curl抓取网页源码,然后以关键字寻找出图片网址.  范例: #include <stdio.h> #include <stdlib.h> #include < ...

  7. Jumony快速抓取网页 --- Jumony使用笔记--icode

    作者:郝喜路   个人主页:http://www.cnicode.com      博客地址:http://haoxilu.cnblogs.com    时间:2014年6月26日 19:25:02 ...

  8. python抓取网页中图片并保存到本地

    #-*-coding:utf-8-*- import os import uuid import urllib2 import cookielib '''获取文件后缀名''' def get_file ...

  9. PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)

    通过curl_setopt()函数可以方便快捷的抓取网页(采集很方便),curl_setopt 是php的一个扩展库 使用条件:需要在php.ini 中配置开启.(PHP 4 >= 4.0.2) ...

随机推荐

  1. C#编程(十四)----------构造函数

    原文链接:http://blog.csdn.net/shanyongxu/article/details/46501683 构造函数 所谓的构造函数就是和类名重名的且没有返回值的方法. class P ...

  2. 技术人生:special considerations that are very important

    For the most part, a lot of what we know about software development can be applied to different envi ...

  3. Exchange2003/2010共存模式环境迁移

    一.我司的exchange2010架构设计基于中心的模式进行.而且基于exchange2010sp3进行. 基于dag三台架构设计进行,截止到5月14日,北京局基于2台dag进行,大连局基于excha ...

  4. Lua date format

    网上有比较复杂的方法:Date Formatting Functions   写了一个非常简单的代码 1: function formatDate(seconds, dateformat) 2: -- ...

  5. 实用ExtJS教程100例-006:ExtJS中Window的用法示例

    在前面几个示例中,我们演示了MessageBox的各种用法,今天这篇文章将演示如何使用Window. 我们首先来创建一个窗口: var win = Ext.create("Ext.windo ...

  6. linux 调用栈打印

    NDK开发的代码打印方式 #include <utils/CallStack.h> extern "C" void dumping_callstack(void);vo ...

  7. [Web 前端 ] Jquery attr()方法 获取或修改 对象的属性值

    cp from : https://blog.csdn.net/gf771115/article/details/18086707 jquery中用attr()方法来获取和设置元素属性,attr是at ...

  8. [转]mysql dual虚拟表

    From : http://thobian.info/?p=1035 虚拟表dual 其实我是今天第一次听说,虽然以前有过它的应用.说不定你也用过哦,看这条sql:select sysdate(); ...

  9. Asp.netMVC中Html.Partial,RenderPartial,Action,RenderAction区别和用法

    https://www.2cto.com/kf/201702/602449.html Partial 和RenderPartial: 这两个的性质都是一样, 只指把一个个View给镶入进来, 只是回传 ...

  10. Andriod源码搜集

    1.一个左侧抽屉式导航NavigationDraw 教程:http://developer.android.com/training/implementing-navigation/nav-drawe ...