ZH奶酪:PHP 使用DOMDocument抓取网页
原文链接: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抓取网页的更多相关文章
- java抓取网页数据,登录之后抓取数据。
最近做了一个从网络上抓取数据的一个小程序.主要关于信贷方面,收集的一些黑名单网站,从该网站上抓取到自己系统中. 也找了一些资料,觉得没有一个很好的,全面的例子.因此在这里做个笔记提醒自己. 首先需要一 ...
- 使用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 ...
- Java 抓取网页中的内容【持续更新】
背景:前几天复习Java的时候看到URL类,当时就想写个小程序试试,迫于考试没有动手,今天写了下,感觉还不错 内容1. 抓取网页中的URL 知识点:Java URL+ 正则表达式 import jav ...
- C语言调用curl库抓取网页图片
思路是先用curl抓取网页源码,然后以关键字寻找出图片网址. #include <stdio.h> #include <stdlib.h> #include <str ...
- [转载]爬虫的自我解剖(抓取网页HtmlUnit)
网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源HtmlUnit包,4行代码就OK啦,例子如下: 1 2 3 4 final WebClient webClie ...
- C语言调用curl库抓取网页图片(转)
思路是先用curl抓取网页源码,然后以关键字寻找出图片网址. 范例: #include <stdio.h> #include <stdlib.h> #include < ...
- Jumony快速抓取网页 --- Jumony使用笔记--icode
作者:郝喜路 个人主页:http://www.cnicode.com 博客地址:http://haoxilu.cnblogs.com 时间:2014年6月26日 19:25:02 ...
- python抓取网页中图片并保存到本地
#-*-coding:utf-8-*- import os import uuid import urllib2 import cookielib '''获取文件后缀名''' def get_file ...
- PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
通过curl_setopt()函数可以方便快捷的抓取网页(采集很方便),curl_setopt 是php的一个扩展库 使用条件:需要在php.ini 中配置开启.(PHP 4 >= 4.0.2) ...
随机推荐
- DirectX全屏游戏中弹出窗口(转)
一直有人问如何在DirectX全屏游戏中弹出窗口就象金山游侠一样.我答应过要给出原码,只是一直没有时间整理,不过现在总算是弄玩了.代码不长,大致作了些注释,但愿你能看懂:)按照我的说明一步步作应该就能 ...
- Maven 使用了一个标准的目录结构和一个默认的构建生命周期。
Maven 使用了一个标准的目录结构和一个默认的构建生命周期. 约定优于配置 当创建 Maven 工程时,Maven 会创建默认的工程结构.开发者只需要合理的放置文件,而在 pom.xml 中不再需要 ...
- springboot中配置druid允许一次执行多条sql
原文:https://blog.csdn.net/jiangjun0130/article/details/77868578 1:在配置文件中不需要指定wall防火墙filter. 配置如下: spr ...
- 11i and R12 Table Count in Different Module
Advertisement Module 11i Tables R12 Tables New Tables AR 551 616 118 BOM 264 337 73 GL 186 309 140 A ...
- SQL:两种获取时间类型日期部分的方法
参考网址:http://www.w3school.com.cn/sql/sql_dates.asp. ), PassedDate, ), , PassedDate), )
- C 格式化显示时间(time.h)
转自:http://www.cnblogs.com/xudong-bupt/p/3550157.html C/C++程序中需要程序显示当前时间,可以使用标准函数strftime. 函数原型:size_ ...
- Hyperledger 项目
https://github.com/hyperledger/fabric.githttps://github.com/hyperledger/blockchain-explorer.githttps ...
- Java学习笔记——IO操作之以图片地址下载图片
以图片地址下载图片 读取给定图片文件的内容,用FileInputStream public static byte[] mReaderPicture(String filePath) { byte[] ...
- Team Viewer 远程链接一直显示-"正在初始化显示参数"
出现这个原因, 原因1: 可能是 通过(mstsc)远程桌面方式运行了teamviewer,被远程控制电脑就会出现这个现象. 可以试一下 服务-teamviewer-属性-登录-本地系统账户 -允许服 ...
- Python调用C/C++程序
编程中会遇到调用其他语言到库,这里记录一下Python调用C++. Python底层是C, 所以调用C还是比较方便.调用C++有些麻烦. Python提供了ctypes, 方便将Python类型转为C ...