转自:http://www.blhere.com/1243.html

使用php simple html dom parser解析html标签
用了一下
PHP Simple HTML DOM Parser
解析HTML页面,感觉还不错,它能创建一个DOM tree方便你解析html里面的内容。用来抓东西挺好的。

附带一个例子,你也到sourceforge下载压缩包看里面的例子:
Scraping data with PHP Simple HTML DOM Parser

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:
view plain copy to clipboard print ?

1
2
3
4
5
6
7
8
9
10
// 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>';
1
2
3
4
5
6
7
8
// 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:
view plain copy to clipboard print ?

1
2
3
4
5
6
7
8
9
// Create DOM from string  
$html = str_get_html('<div id="simple">Simple</div><div id="parser">Parser</div>');  
 
)->class = 'bar';  
 
)->innertext = 'Foo';  
 
// Output: <div id="simple">Foo</div><div id="parser" class="bar">Parser</div>  
echo $html;
1
2
3
4
5
6
// Create DOM from string
$html = str_get_html('<div id="simple">Simple</div><div id="parser">Parser</div>');
)->class = 'bar';
)->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?
view plain copy to clipboard print ?

1
echo file_get_html('http://www.yahoo.com/')->plaintext;
1
echo file_get_html('http://www.yahoo.com/')->plaintext;

In the package files of this parser ([url]http://simplehtmldom.sourceforge.net/[/url]) 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:
view plain copy to clipboard print ?

1
2
3
4
5
6
7
8
9
10
$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)  
{  
).'. '.$info->plaintext."<br />\n";  
}
1
2
3
4
5
6
7
8
$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)
{
).'. '.$info->plaintext."<br />\n";
}

NOTE Make sure to include the parser before using any functions of it:
view plain copy to clipboard print ?

Php代码

1
include 'simple_html_dom.php';
1
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: [下载]

使用php simple html dom parser解析html标签的更多相关文章

  1. PHP Simple HTML DOM Parser Manual-php解析DOM

    PHP Simple HTML DOM Parser Manual http://www.lupaworld.com/doc-doc-api-770.html PHP Simple HTML DOM ...

  2. php爬虫学习笔记1 PHP Simple HTML DOM Parser

    常用爬虫. 0. Snoopy是什么? (下载snoopy)   Snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务.   Snoopy的一些特点:   * ...

  3. PHP Simple HTML DOM解析器

    一直以来使用php解析html文档树都是一个难题.Simple HTML DOM parser 帮我们很好地解决了使用 php html 解析 问题.可以通过这个php类来解析html文档,对其中的h ...

  4. PHP Simple HTML DOM解析器使用入门

    http://www.cnphp.info/php-simple-html-dom-parser-intro.html 一直以来使用php解析html文档树都是一个难题.Simple HTML DOM ...

  5. PHP Simple HTML DOM 使用

    下载地址:https://github.com/samacs/simple_html_dom 文档地址 http://simplehtmldom.sourceforge.net/ 一直以来使用php解 ...

  6. EasyUI基础入门之Parser(解析器)

    前言 JQuery EasyUI提供的组件包含功能强大的DataGrid,TreeGrid.面板.下拉组合等.用户能够组合使用这些组件,也能够单独使用当中一个.(使用的形式是以插件的方式提供的) Ea ...

  7. EasyUI Parser 解析器

    Parser(解析器)应用场景 1,自动调用parser 只要我们书写相应的class,easyui就能成功的渲染页面,这是因为解析器在默认情况下,会在dom加载完成的时候($(docunment). ...

  8. 用JAXP的dom方式解析XML文件

    用JAXP的dom方式解析XML文件,实现增删改查操作 dom方式解析XML原理 XML文件 <?xml version="1.0" encoding="UTF-8 ...

  9. Dom方式解析XML

    public class TestXML { public static void main(String[] args) throws SAXException, IOException { //D ...

随机推荐

  1. day65-test

    目录 一.点击事件控制标签颜色 二.实现点击次数,变换页面标签的颜色 三.周期性实现颜色的旋转变色 练习题 一.点击事件控制标签颜色 1.有 红.黄.蓝 三个按钮,以及一个200x200矩形框box, ...

  2. 好用的Win10快捷键

    好用的Win10快捷键 Top 01 基础按键 Win+E: 打开"资源管理器". Win+R: 打开"运行"对话框. Win+L: 锁定当前用户. Win+D ...

  3. 超干货!Cassandra Java堆外内存排查经历全记录

    背景 最近准备上线cassandra这个产品,同事在做一些小规格ECS(8G)的压测.压测时候比较容易触发OOM Killer,把cassandra进程干掉.问题是8G这个规格我配置的heap(Xmx ...

  4. textarea高度自动增高

    <!--随着textarea 输入内容 自动增加高度--> <script type="text/javascript"> $(".input_t ...

  5. git学习记录——远程仓库(说白了就是代码放到githup上)

    远程仓库 现在讲述的这些SVN都已经做到了,并没什么稀奇的地方 所以这节课赘述的是杀手级的东西——远程仓库githup ssh-keygen -t rsa  -C "xxxxxxxxxxx@ ...

  6. zoj 1001 python起步

    /*赶角还是挺好的....*/ import sys for line in sys.stdin: a=line.split() print int(a[0])+int(a[1])

  7. codeforces 545E E. Paths and Trees(单源最短路+总权重最小)

    E. Paths and Trees time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  8. 蛋疼的JavaScript(二)this

    ### 烦人的this JavaScript的表现与之前学的任何传统的语言都不一样,这个就很烦人,这几天把这个坑扫除了 ### this到底指向什么 总的来说,总结下,this其实取决于调用者的上下文 ...

  9. 【大数据】Hadoop常用启动命令

    Hadoop常用启停命令 最近在装大数据环境,不知由于年纪大的问题还是笨的缘故,老师记不住一些常用命令,在这里就单独记一下Hadoop常用的启停命令.Hadoop常用的启停命令都在hadoop/sbi ...

  10. Python学习(二) 基础语法之初看python

    Python 标识符 略 Python保留字符 一大堆,说了未必记得住,编码过程中慢慢去记住. 行和缩进 这个要说一下,学习Python与其他语言最大的区别就是,Python的代码块不使用大括号({} ...