原文链接: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. 写一个限制上传文件大小和格式的jQuery插件

    在客户端上传文件,通常需要限制文件的尺寸和格式,最常用的做法是使用某款插件,一些成熟的插件的确界面好看,且功能强大,但美中不足的是:有时候会碰到浏览器兼容问题.本篇就来写一个"原生态&quo ...

  2. 在ASP.NET MVC中使用Boostrap实现产品的展示、查询、排序、分页

    在产品展示中,通常涉及产品的展示方式.查询.排序.分页,本篇就在ASP.NET MVC下,使用Boostrap来实现. 源码放在了GitHub: https://github.com/darrenji ...

  3. mysql time zone时区的错误解决

    错误提示: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zon ...

  4. Java String练习题及答案

    1. 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕 /** * 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕 */ publ ...

  5. Android Service总结04 之被绑定的服务 -- Bound Service

    Android Service总结04 之被绑定的服务 -- Bound Service 版本 版本说明 发布时间 发布人 V1.0 添加了Service的介绍和示例 2013-03-17 Skywa ...

  6. CATransition 实践

    时间差不够,导致闪屏 CATransition *animation = [CATransition animation]; animation.delegate = self; animation. ...

  7. java去除数组中重复的元素方法总结

    /* * ArrayUnique.java * Version 1.0.0 * Created on 2017年12月16日 * Copyright ReYo.Cn */ package reyo.s ...

  8. 利用svn log命令实现的资源版本更新

    无论页游或是手游都需要经常进行更新,而每一次更新几乎都是一部血泪吏.这里重点介绍一下前端资源打包的简化操作.目前2D手游主流都采用了cocos2d-x 绑lua的做法,因为lua相当于一种资源可以进行 ...

  9. 解决Installation error: INSTALL_FAILED_VERSION_DOWNGRADE错误

    Installation error: INSTALL_FAILED_VERSION_DOWNGRADE 说明你手机里已经装的软件版本比你要安装的软件版本要高,所以不能安装. 你只要删除你安装的应用便 ...

  10. GridView和SimpleAdapter实现网格布局

    android:horizontalSpacing 元素之间的水平间距 android:verticalSpacing     元素之间的垂直间距 android:numColumns         ...