用了一下
解析HTML页面,感觉还不错,它能创建一个DOM tree方便你解析html里面的内容。用来抓东西挺好的。
附带一个例子,你也到sourceforge下载压缩包看里面的例子:
<!---->
<!---->
<!---->
Scraping data with PHP Simple HTML DOM Parser
<!---->
Stumble Upon it!
<!---->
Save to Del.icio.us
(9
saves)
<!---->
Share on Twitter!
<!---->
<!---->
PHP Simple HTML DOM Parser
,
written in PHP5+, allows you to manipulate HTML in a very easy way.
Supporting invalid HTML, this parser is better then other PHP scripts
using complicated regexes to extract information from web pages.
Before getting the necessary info, a DOM should be created from
either URL or file. The following script extracts links & images
from a website:
-
- $html
= file_get_html(
'http://www.microsoft.com/'
);
-
-
- foreach
(
$html
->find(
'a'
)
as
$element
)
- echo
$element
->href .
'<br>'
;
-
-
- foreach
(
$html
->find(
'img'
)
as
$element
)
- echo
$element
->src .
'<br>'
;
// Create DOM from URL or file
$html = file_get_html('http://www.microsoft.com/');
// Extract links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
// Extract images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
The parser can also be used to modify HTML elements:
-
- $html
= str_get_html(
'<div id="simple">Simple</div><div id="parser">Parser</div>'
);
-
- $html
->find(
'div'
, 1)->
class
=
'bar'
;
-
- $html
->find(
'div[id=simple]'
, 0)->innertext =
'Foo'
;
-
-
- echo
$html
;
// Create DOM from string
$html = str_get_html('<div id="simple">Simple</div><div id="parser">Parser</div>');
$html->find('div', 1)->class = 'bar';
$html->find('div[id=simple]', 0)->innertext = 'Foo';
// Output: <div id="simple">Foo</div><div id="parser" class="bar">Parser</div>
echo $html;
Do you wish to retrieve content without any tags?
- echo
file_get_html(
'http://www.yahoo.com/'
)->plaintext;
echo file_get_html('http://www.yahoo.com/')->plaintext;
In the package files of this parser
(http://simplehtmldom.sourceforge.net/) you can find some scraping
examples from digg, imdb, slashdot. Let’s create one that extracts the
first 10 results (titles only) for the keyword “php” from Google:
- $url
=
'http://www.google.com/search?hl=en&q=php&btnG=Search'
;
-
-
- $html
= file_get_html(
$url
);
-
-
- foreach
(
$html
->find(
'a[class=l]'
)
as
$key
=>
$info
)
- {
- echo
(
$key
+ 1).
'. '
.
$info
->plaintext.
"<br />\n"
;
- }
$url = 'http://www.google.com/search?hl=en&q=php&btnG=Search';
// Create DOM from URL
$html = file_get_html($url);
// Match all 'A' tags that have the class attribute equal with 'l'
foreach($html->find('a[class=l]') as $key => $info)
{
echo ($key + 1).'. '.$info->plaintext."<br />\n";
}
NOTE
Make sure to include the parser before using any functions of it:
- include
'simple_html_dom.php'
;
include 'simple_html_dom.php';
For more information regarding the usage of this function consider
checking the ‘PHP Simple HTML Dom Parser’ Manual. To download the
package files use the following URL: http://sourceforge.net/project/showfiles.php?group_id=218559
.
分享到:
相关推荐
simple_html_dom中文解析手册
php-simple-html-dom-parser, PHP简单的HTML DOM解析器适应 Composer 和 PSR 0 php-simple-html-dom-parser版本 1.5.2针对 Composer 和 PSR-0的自适应:用PHP5 编写的HTML解析器允许你以非常简单的方式操作 HTML !...
高效快速分析和获取HTML内容,对抓取过来的内容进行分析和特定内容提取很方便
一直以来使用php解析html文档树都是一个难题。Simple HTML DOM parser 帮我们很好地解决了这个问题。可以通过这个php类来解析html文档,对其中的html元素进行操作 (PHP5+以上版本)。
由于基于php模块dom,所以在解析html时的效率比PHP Simple HTML DOM Parser快好几倍。注意:html代码必须是utf-8编码字符,如果不是请转成utf-8如果有乱码的问题参考: ://www.fwolf.com/blog/post/314现在支持...
Laravel开发-php-simple-html-dom-parser PHP简单HTML DOM解析器包的Laravel包装。
一直以来使用php解析html文档树都是一个难题。Simple HTML DOM parser 帮我们很好地解决了使用 php html 解析 问题。可以通过这个php类来解析html文档,对其中的html元素进行操作 (PHP5+以上版本)。
simplehtmldom是用于PHP的快速可靠HTML DOM解析器。 主要特征 纯粹基于PHP的DOM解析器(无需XML扩展名)。 适用于格式正确且已损坏HTML文档。 加载网页,本地文件和文档字符串。 支持CSS选择器。 要求 simple...
最近想用php写一个爬虫,就需要解析html,在sourceforge上找到一个项目叫做PHP Simple HTML DOM Parser,它可以以类似jQuery的方式通过css选择器来返回指定的DOM元素,功能十分强大。 首先要在程序的开始引入simple_...
一直以来使用php解析html文档树都是一个难题。Simple HTML DOM parser 帮我们很好地解决了这个问题。可以通过这个php类来解析html文档,对其中的html元素进行操作 (PHP5+以上版本)
用PHP编写的XML DOM样式解析器类。 此类可以使用expat或regex导入XML数据。
HTMLDoc:PHP HTML文档解析器和Minifier 基于令牌HTML文档解析器和压缩器,用PHP编写。 描述 一个HTML解析器,主要用于缩小HTML文档,它还可以查询文档结构,从而提取属性和textnode值。 这两个解析器都是围绕标记...
#Sublime 文字引述有了这个应用程序,你基本上可以阅读一些不错的励志名言,我用一个名为Simple HTML DOM Parser的库做了一个解析器,用 PHP lenguaje 编写,以填充数据库,我还创建了一个选项来添加你自己的短语,...
OLX广告解析器 开始工作之前,您需要在links.txt文件中填写指向所宣传广告的链接 二手第三方图书馆 使用文档 档案文件 文件 描述 index.php 主要可执行文件 links.txt ...核心/ simple_html_dom.php
DTD是一种保证XML文档格式正确的有效方法,可以比较XML文档和DTD文件来看文档是否符合规范,元素和标签使用是否正确。一个DTD文档包含:元素的定义规则,元素间关系的定义规则,元素可使用的属性,可使用的实体或...