使用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 ...
随机推荐
- 第三方博客同步xmlrpc、rest、API等相关的文章网址记录
http://answers.microsoft.com/en-us/windowslive/forum/writer-wlsettings/i-am-encountering-a-problem-s ...
- 010-利用Selenium+python自动输入博客账号密码登录
from selenium import webdriver import time def OpenUrl(): # 访问网址 driver.get(url) def Login(): # 查询登录 ...
- gulp入门之常见处理方式(三)
整合 streams 来处理错误 默认情况下,在 stream 中发生一个错误的话,它会被直接抛出,除非已经有一个时间监听器监听着 error时间. 这在处理一个比较长的管道操作的时候会显得比较棘手. ...
- JasperReports报表组15
组在JasperReports的协助组织对报告的数据以逻辑方式.报告组代表连续记录的数据源中有一些共同点,比如某个报表字段的值的序列.报告组由<group>元素定义.一个报表可以有任意数量 ...
- 在Linux中常用的启动引导工具:grub和lilo
在Linux和WINDOWS两系统并存时就需要安装GRUB(Grand Unified Bootloader),GRUB被广泛地用于替代lilo,GRUB支持在启动时使用命令行模式,支持md5加密保护 ...
- input光标错位
文档结构 <div class="noteWrap"> <input type="text" placeholder="写留言&qu ...
- 读取复杂结构的yml配置项
1.yml配置项示例:(List的集合在第一项前面加 “-”) rabbitmqsetting: exchangeList: - name: e1 type: topic bindingList: - ...
- 2018-2019年中国CDN市场发展报告:阿里云成为中国CDN市场的领军者
近日,权威ICT市场咨询机构计世资讯(CCW Research)发布<2018-2019年中国CDN市场发展报告>,报告显示,当前,随着新型信息技术在中国不断应用,以及互联网化新业务的快速 ...
- CF 549B Looksery Party
题面 解题思路 如果a数组全部>0,那么都不去即可.从这个角度出发,每次选出a[i]为0的,让它们去更新a数组,相当于拓补排序. 代码 #include<iostream> #inc ...
- SSM11-Redis---jedis的使用方法以及缓存同步
1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.E3-content-Service工程中. 1.1. 连接单机版 ...