使用php simple html dom parser解析html标签
转自: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标签的更多相关文章
- 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 ...
- php爬虫学习笔记1 PHP Simple HTML DOM Parser
常用爬虫. 0. Snoopy是什么? (下载snoopy) Snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务. Snoopy的一些特点: * ...
- PHP Simple HTML DOM解析器
一直以来使用php解析html文档树都是一个难题.Simple HTML DOM parser 帮我们很好地解决了使用 php html 解析 问题.可以通过这个php类来解析html文档,对其中的h ...
- PHP Simple HTML DOM解析器使用入门
http://www.cnphp.info/php-simple-html-dom-parser-intro.html 一直以来使用php解析html文档树都是一个难题.Simple HTML DOM ...
- PHP Simple HTML DOM 使用
下载地址:https://github.com/samacs/simple_html_dom 文档地址 http://simplehtmldom.sourceforge.net/ 一直以来使用php解 ...
- EasyUI基础入门之Parser(解析器)
前言 JQuery EasyUI提供的组件包含功能强大的DataGrid,TreeGrid.面板.下拉组合等.用户能够组合使用这些组件,也能够单独使用当中一个.(使用的形式是以插件的方式提供的) Ea ...
- EasyUI Parser 解析器
Parser(解析器)应用场景 1,自动调用parser 只要我们书写相应的class,easyui就能成功的渲染页面,这是因为解析器在默认情况下,会在dom加载完成的时候($(docunment). ...
- 用JAXP的dom方式解析XML文件
用JAXP的dom方式解析XML文件,实现增删改查操作 dom方式解析XML原理 XML文件 <?xml version="1.0" encoding="UTF-8 ...
- Dom方式解析XML
public class TestXML { public static void main(String[] args) throws SAXException, IOException { //D ...
随机推荐
- CentOS 6.8 Linux系统U盘制作启动项
1.下载CentOS 6.8镜像文件: 2.下载地址:http://man.linuxde.net/download/CentOS_6_8 3.准备一个U盘,最好8G的: 4.下载UltraISO盘制 ...
- Simple implementation and results of genetic algorithm.
This experiment was done for the final assignment of my Professional English class. This part has be ...
- 微服务开源生态报告 No.10
「微服务开源生态报告」,汇集各个开源项目近期的社区动态,帮助开发者们更高效的了解到各开源项目的最新进展. 社区动态包括,但不限于:版本发布.人员动态.项目动态和规划.培训和活动. 非常欢迎国内其他微服 ...
- 基于MaxCompute的媒体大数据开放平台建设
摘要:随着自媒体的发展,传统媒体面临着巨大的压力和挑战,新华智云运用大数据和人工智能技术,致力于为媒体行业赋能.通过媒体大数据开放平台,将媒体行业全网数据汇总起来,借助平台数据处理能力和算法能力,将有 ...
- 【python之路39】Python 正则表达式
Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. r ...
- 不同尺寸设计图 rem 断点数据记录
320px宽的设计图 @media screen and (min-width: 320px) { html { font-size: 100px; } } @media screen and (mi ...
- Intent 传递Map数据
android开发默认情况下,通过Bundle bundle=new Bundle();传递值是不能直接传递map对象的,解决办法: 第一步:封装自己的map,实现序列化即可 /** *序列化map供 ...
- MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // 转换成多部分request异常
原来是需要在springmvc中配置 <!-- multipartResolver上传文件 --> <bean id="multipartResolver" cl ...
- JSP内置对象解析
out对象:(PrintWriter类的实例) 用来向客户端输出信息,除了输出各种信息外还负责对缓冲区进行管理: 主要方法: print / println void 输出数据 newLine() v ...
- IO流6 --- FileReader中使用read(char[] cbuf)读入数据 --- 技术搬运工(尚硅谷)
FileReader 字符输入流 @Test public void test2(){ File file = new File("hello.txt"); FileReader ...