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) ...
随机推荐
- IOS开发之——objective-c与javascript交互
原文:http://blog.csdn.net/pjk1129/article/details/6936545 在写 JavaScript 的时候,可以使用一个叫做 window 的对象,像是我们想要 ...
- 查看webservice服务下的所有方法和参数类型
方法:直接在IE浏览器中输入webservice的地址,查看返回的XML数据即可. 效果如下:
- Codeforces Round #131 Div1 B
Problem 给出Ai(i∈[0,9]).表示每一个数字至少须要出现多少次.问有多少个数满足下面三个条件:1. 数至多有N位:2. 数不含有前导0:3. 每一个数 i 出现的次数不少于Ai(mod ...
- 尼基塔第一季/全集Nikita迅雷下载
本季Nikita Season 1 第一季(2010)看点:尼基塔曾经是一个性格叛逆的问题少女,因为犯下重罪被处以死刑.一家秘密间谍机构将尼基塔从死牢里救了出来,伪造了她的死亡,将她训练成了一名间谍和 ...
- ios成长之每日一遍(day 1)
Hello world开始. 这里不讨论如何创建项目导入项目.由于趁上班时间打酱油所以也不谈细节, 只谈具体项目的实现与关键流程的解析, 只供本人实际程况使用.不喜请移驾. 首先来谈谈 AppDele ...
- 详细解读Volley(一)—— 基本Request对象 & RequestQueue
Volley它非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕.所以不建议用它去进行下载文件.加载大图的操作.有人可能会问,如 ...
- HTML5 Geolocation API地理定位整理(一)
HTML5 Geolocation API 用于获得用户的地理位置. 鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的. 浏览器支持 Internet Explorer 9+, ...
- ASP,ASP.net,JSP语法、内置对象对比
1 各自的HelloWord版本 1.1 ASP <% Response.Write("hello asp") %> 文件名为test.asp. 1.2 ASP.ne ...
- OC-字符串中大小写字母转换
一般语言中的大小写转换都会提供的有默认的函数,不过闲来无事,简单的模仿实现了一下: 系统中默认的大小写转换: NSString *name=@"博客园-FlyElephant"; ...
- Edit Distance leetcode java
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...