转自: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. 深喉起底APP线下预装市场,如何一夜间拥有千万用户

    注:预装对于中国的移动互联网创业者有多重要?i黑马知道这样一个内幕,某商务告诉我他们公司的前2000万用户就是靠预装打下来的,总部在北京,直接派驻商务长期扎根在深圳搞定手机厂商.而这家公司初期发展得益 ...

  2. Python基础-列表、元组、字典、字符串(精简解析),全网最齐全。

    一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...

  3. struts2-环境搭建-访问流程-配置详解-常量配置-类详解

    1 struts2概述 1.1 概念  1.2 struts2使用优势 自动封装参数 参数校验 结果的处理(转发|重定向) 国际化 显示等待页面 表单的防止重复提交 struts2具有更加先进的架构以 ...

  4. Effective Modern C++  条款1:理解模板型别推导

    成百上千的程序员都在向函数模板传递实参,并拿到了完全满意的结果,而这些程序员中却有很多对这些函数使用的型别是如何被推导出的过程连最模糊的描述都讲不出来. 但是当模板型别推导规则应用于auto语境时,它 ...

  5. 全景还原报错现场 | 应用实时监控 ARMS 上线用户行为回溯功能

    随着前端技术日新月异迅猛发展,为了实现更好的前端性能,最大程度提高用户体验,支持单页应用的框架逐渐占领市场,如众所周知的React,Vue等等.但是在单页应用的趋势下,快速定位并解决JS错误却成为一大 ...

  6. 在vscode中使用字体Cascadia Code

    下载字体 下载地址:github:https://github.com/microsoft/cascadia-code/releases 安装字体(windows10) 设置 > 个性化 > ...

  7. Java review-basic3

    Mutexes, ReadWriteLock, ArrayBlockingQueue, Thread pools, LinkedList vs ArrayList, Object Pooling, R ...

  8. linux如何将分组权限置为空

    两种方法 方法一:使用-符号 chmod g=- monkey.py#可以单独指定一个 方法二:简写方式,用0表示 chmod 740 monkey.py#必须同时指定三个的权限

  9. 学习JDK1.8集合源码之--TreeMap

    1. TreeMap简介 TreeMap继承自AbstractMap,实现了NavigableMap.Cloneable.java.io.Serializable接口.所以TreeMap也是一个key ...

  10. 【同余最短路】洛谷 P2662 牛场围栏

    关于同余最短路的部分 [同余最短路]P3403跳楼机/P2371墨墨的等式 [P2662牛场围栏] 题目背景 小L通过泥萌的帮助,成功解决了二叉树的修改问题,并因此写了一篇论文, 成功报送了叉院(羡慕 ...