转自: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. JS基础之EL表达式

    一.EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数 ...

  2. Scrapy框架Crawler模板爬虫

    1.创建一个CrawlerSpider scrapy genspider -t crawl wx_spider 'wxapp-union.com' #导入规则 from scrapy.spiders ...

  3. LINUX超级用户(权限)在系统管理中的作用

    1.对任何文件.目录或进程进行操作: 但值得注意的是这种操作是在系统最高许可范围内的操作:有些操作就是具有超级权限的root也无法完成: 比如/proc 目录,/proc 是用来反应系统运行的实时状态 ...

  4. [原创]Machine Learning/机器学习 文章合集

    转载请注明出处:https://www.codelast.com/ ➤ 用人话解释机器学习中的Logistic Regression(逻辑回归) ➤ 如何防止softmax函数上溢出(overflow ...

  5. TZOJ 3522 Checker Challenge(深搜)

    描述 Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so th ...

  6. https://vjudge.net/problem/2198220/origin

    https://vjudge.net/problem/2198220/origin枚举等差数列第一个和第二个,然后二分确定数列后面是否存在,复杂度比较玄学,卡过了. #include<iostr ...

  7. 用JetBrains PyCharm 2017.2创建运行Django程序

    在JetBrains PyCharm 2017.2里选择 文件(F) 新项目 点击  三角形  运行 修改Urls.py """S14Djngo URL Configur ...

  8. @ font-face 引入本地字体文件

    @font-face { font-family: DeliciousRoman; src: url('…/Delicious-Roman.otf'); font-stretch: condensed ...

  9. Mac安装软件新方法:Homebrew-cask

    Homebrew是Ruby社区极富想象力的一个作品,使得Mac下安装Mysql等常用包不再困难.那么,是否也可以通过brew install mysql这样简单的方式来安装chrome浏览器? 近期, ...

  10. LintCode_100 删除排序数组中的重复数字 ||

    题目 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2,3]. C++代码 int rem ...