使用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 ...
随机推荐
- Dock镜像初探索
一.安装CentOS版DockerCE 1.1 卸载旧的版本 yum remove docker \ docker-client \ docker-client-latest \ docker-com ...
- redis学习笔记05-发布订阅模式
由于redis消息队列并不支持多播机制,即生产者产出一次,由中间件复制发送给多个消费者的机制,为了弥补这个不足,单独引入了发布订阅模式的模块.使用方式如下: #生产者 import redis cli ...
- pytorch dataloader 取batch_size时候 出现bug
1.RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 342 and 2 ...
- LA2238 Fixed Partition Memory Management
题目大意: m(m<=10)个内存区域,n(n<=50)个程序.找出一个方案来,使得平均结束时刻尽量小.题目保证有解. 同一个程序运行在不同大小的内存区域内,其运行时间不同.(注意,这里说 ...
- Leetcode461Hamming Distance汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = 1, y ...
- 洛谷 2890 [USACO07OPEN]便宜的回文Cheapest Palindrome
传送门 一道最简单的区间dp,然而我还是抄了题解. //Twenty #include<algorithm> #include<iostream> #include<cs ...
- centos apache安装oracle扩展
参考网址: http://blog.csdn.net/a82168506/article/details/11763989 步骤如下: 下载安装包,下载地址.(我下载的11.1版本) http://w ...
- NYoj536 矩阵链乘
经典问题没啥说的 #include<stdio.h> #include<string.h> #define max 100+1 #define min(a,b) (a<b ...
- 读书笔记--Head First JavaScript 目录
1.交互式网络 2.存储数据 3.探索客户端 4.决策 5.循环 6.函数 7.表单与验证 8.驾驭网页 9.为数据带来生命 10.创建自定义对象 11.除错务尽 12.动态数据
- fastjson string[]转 json字符串
@RequestMapping(value = "/friendlink_list/province_list", produces = {"application/js ...