PHP Simple HTML DOM Parser – 用PHP轻松解析HTML DOM

这个玩意,用来采集挺好的。别的就不废话了。

项目地址:http://simplehtmldom.sourceforge.net/

简介

  • 使用PHP5实现的HTML DOM解析器。让你能够使用PHP5轻松操纵HTML DOM。
  • 支持PHP5及以上版本。
  • jQuery一样使用选择器获取HTML标签。
  • 一行代码获取HTML中的内容。

快速入门

1、如何获取HTML节点?

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
 
// Find all images
foreach($html->find('img') as $element)
      
echo $element->src . '<br>';
 
// Find all links
foreach($html->find('a') as $element)
      
echo $element->href . '<br>';

2、如何修改HTML节点?

// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
 
$html->find('div', 1)->class = 'bar';
 
$html->find('div[id=hello]', 0)->innertext = 'foo';
 
echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>

3、从HTML中获取内容

// Dump contents (without tags) from HTML
echo file_get_html('http://www.google.com/')->plaintext;

下载&文档

下载地址:http://sourceforge.net/projects/simplehtmldom/files/

在线文档:http://simplehtmldom.sourceforge.net/manual.htm

Tags : ,

Google Reader Yahoo Facebook Twitter Digg FriendFeed Delicious Google Translate
这篇日志发表于2011年08月1日 02:45。 你可以订阅该日志的所有评论通过 RSS 2.0。 你可以发表评论,或者引用通告

发表评论

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:

(Ctrl+Enter)

XHTML:你可以使用这些标签:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>