转自: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. STL中的unique()和lower_bound ,upper_bound

    unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...

  2. 再谈MFC学习——模态对话框的数据传递

    学习c++的目的就是为了使用封装好的类.而最最经典的封装类当然是微软的MFC窗体程序类. 学习MFC编程之前要学习c++的主要知识,掌握c++的基本编程思想. 以下就看下我学习的MFC模态对话框的数据 ...

  3. Jmeter环境搭建及目录介绍

    Jmeter环境搭建及目录介绍 运行环境:jmeter是基于java语言的,所有需要准备Java的JDK环境,并添加到环境变量中. 一.环境搭建及配置 1.JMeter的下载: 官网:http://j ...

  4. 如何把win10自带输入法改为简体中文

    win10设置为中文简体 先找到win10的设置,然后下面按照图示操作,很简单 点击每一个红色的方框既能够到达---------->>>中文简体  目的地 2 会了吗,你个小傻瓜

  5. TZ_16_Vue的v-for、v-if、v-show、v-bind、watch

    1.v-for 遍历数据渲染页面是非常常用的需求,Vue中通过v-for指令来实现. 1>遍历一个users数组 <!-- ve-for --> <ul> <li ...

  6. 在网站制作过程中发现的block和inline-block不同。

    inline-block,简单来说就是在CSS中通过display:inline-block对一个对象指定inline-block属性,可以将对象呈递为内联对象,但是对象的内容作为块对象呈递.有时既希 ...

  7. 做移动应用使用地图API时需要注意的问题

    最近在做一个基于地点提醒的移动应用,当初考虑大家都心知肚明的原因,谨慎的选择了百度地图,现在想想其实完全没有必要,好的应用本来就不分国界的,最后可能还是得换回Google地图.毕竟Google地图在技 ...

  8. ADO.NET实体数据模型

    本文说一下如何使用ADO.NET实体数据模型,并解释一些概念. 1,首先你要建立一个数据库.比如我在SQL2005上面建立了数据库student,包含两个表: 2,然后再项目上添加新建项: 3,打开新 ...

  9. 在多版本python的pip的安装与对应包的安装

    最近花了好长时间在搞这个,由于Deepin下python有两个版本,并且都没有安装pip,之前的博文默认安装pip给python2.7,结果各种问题,在此将之前走过的弯路整合起来: 首先,安装pip ...

  10. Dockerfile 编写

    转: https://blog.fundebug.com/2017/05/15/write-excellent-dockerfile/如何编写最佳的Dockerfile 译者按: Dockerfile ...