使用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 ...
随机推荐
- html如何设置表格单元格内容垂直居中?
父元素设置为表格的单元格元素td,而在表格单元格中的元素设置vertical-align: middle; 对父容器(td)使用:display: table-cell 其内子元素使用:vertica ...
- Vue. 之 刷新当前页面,重载页面数据
Vue. 之 刷新当前页面,重载页面数据 如下截图,点击左侧不同的数据,右侧根据左侧的KEY动态加载数据.由于右侧是同一个页面,在进行路由跳转后,不会再次刷新数据. 解决方案: 右侧的页面中 scri ...
- 系统io统计
$ cat /proc/diskstats sda sda1 sda2 gg- gg- gg- 主号 次号 名称 成功读 合并读 扇区读 读时间 每一列的含义分别为: 第一列为 设备号 (nu ...
- NDK(23) 使用CMake 构建 c/c++代码库
1.官网 https://developer.android.com/studio/projects/add-native-code.html 2.android studio 安装相关工具 在打开的 ...
- 微信Web开发者工具报错:net::ERR_BLOCKED_BY_CLIENT
今天在微信调试琳德的拼团,用了挺久之后,输入什么测试地址:发现页面空白,console提示身份验证网址错误,错误类型为:net::ERR_BLOCKED_BY_CLIENT, 尝试了好多次.以为是我的 ...
- vue多页面项目配置
全局配置 打开 ~\build\webpack.base.conf.js ,找到entry,添加多入口 entry: { main: './src/main.js', main2: './src/ma ...
- jeecms怎么修改后台访问路径?
1,修改后台访问路径: 如:http://localhost:8080/jeeadmin/jeecms/login.do 改:http://localhost:8080/hailou/index.do ...
- Markdown文档使用
Markdown使用 一.markdown标题:1级-6级 一级 #空格 二级 ##空格 三级 ###空格 ... 六级 ######空格 二.代码块 print("hello world! ...
- JQ效果 透明图片覆盖动画
效果图呈上 先说思路 1,一个固定的框架,有两张图片,一张是狗狗的,一张是练习方式,想把做好的练习方式隐藏 2,效果上想要从下面滑动出来,所以透明框定位在下面 3,整理需要的东西,缓慢升起需要动画效果 ...
- Swift 和 Objective-C 混编后对ipa包大小的影响
https://my.oschina.net/ilrrong/blog/800923 最近用Swift对以前写的一个应用进行重写,使用了Swift和Objective-C的混编,提交审核后发现比以前大 ...