欢迎光临
我们一直在努力

phpQuery—基于jQuery的PHP实现

Query的选择器之强大是有目共睹的,phpQuery 让php也拥有了这样的能力,它就相当于服务端的jQuery。

先来看看官方简介:

phpQuery is a server-side, chainable, CSS3 selector driven Document Object Model (DOM) API based on jQuery JavaScript Library.
Library is written in PHP5 and provides additional Command Line Interface (CLI).

存在的意义

我们有时需要抓取一个网页的内容,但只需要特定部分的信息,通常会用正则来解决,这当然没有问题。正则是一个通用解决方案,但特定情况下,往往有更简单快 捷的方法。比如你想查询一个编程方面的问题,当然可以使用Google,但stackoverflow 作为一个专业的编程问答社区,会提供给你更多,更靠谱的答案。

对于html页面,不应该使用正则的原因主要有3个

1、编写条件表达式比较麻烦
尤其对于新手,看到一堆”不知所云”的字符评凑在一起,有种脑袋都要炸了的感觉。如果要分离的对象没有太明显的特征,正则写起来更是麻烦。

2、效率不高
对于php来说,正则应该是没有办法的办法,能通过字符串函数解决的,就不要劳烦正则了。用正则去处理一个30多k的文件,效率不敢保证。

3、有phpQuery
如果你使用过jQuery,想获取某个特定元素应该是轻而易举的事情,phpQuery让这成为了可能。

浅析phpQuery

phpQuery是基于php5新添加的DOMDocument。而DOMDocument则是专门用来处理html/xml。它提供了强大xpath选 择器及其他很多html/xml操作函数,使得处理html/xml起来非常方便。那为什么不直接使用呢?这个,去看一下官网的函数列表 就知道了,如果对自己的记忆力很有信心, 不妨一试。

html文件test.html:
复制代码 代码如下:
[html]
<div class="thumb" id="Thumb-13164-3640" style="position: absolute; left: 0px; top: 0px;">
<a href="/Spiderman-City-Drive">
<img src="/thumb/12/Spiderman-City-Drive.jpg" alt="">
<span class="GameName" id="GameName-13164-3640" style="display: none;">Spiderman City Drive</span>
<span class="GameRating" id="GameRating-13164-3640" style="display: none;">
<span style="width: 68.14816px;"></span>
</span>
</a>
</div>
<div class="thumb" id="Thumb-13169-5946" style="position: absolute; left: 190px; top: 0px;">
<a href="/Spiderman-City-Raid">
<img src="/thumb/12/Spiderman-City-Raid.jpg" alt="">
<span class="GameName" id="GameName-13169-5946" style="display: none;">Spiderman – City Raid</span>
<span class="GameRating" id="GameRating-13169-5946" style="display: none;">
<span style="width: 67.01152px;"></span>
</span>
</a>
</div>
[/html]
php处理:
复制代码 代码如下:
[php]
<?php
include(‘phpQuery-onefile.php’);

$filePath = ‘test.html’;
$fileContent = file_get_contents($filePath);
$doc = phpQuery::newDocumentHTML($fileContent);
phpQuery::selectDocument($doc);
$data = array(
‘name’ => array(),
‘href’ => array(),
‘img’ => array()
);
foreach (pq(‘a’) as $t) {
$href = $t -> getAttribute(‘href’);
$data[‘href’][] = $href;
}
foreach (pq(‘img’) as $img) {
$data[‘img’][] = $domain . $img -> getAttribute(‘src’);
}
foreach (pq(‘.GameName’) as $name) {
$data[‘name’][] = $name -> nodeValue;
}
var_dump($data);
?>
[/php]

上面的代码中包含了取属性和innerText内容(通过nodeValue取)。
输出:
复制代码 代码如下:
[php]
array (size=3)
‘name’ =>
array (size=2)
0 => string ‘Spiderman City Drive’ (length=20)
1 => string ‘Spiderman – City Raid’ (length=21)
‘href’ =>
array (size=2)
0 => string ‘http://www.z1988.com/Spiderman-City-Drive’ (length=40)
1 => string ‘http://www.z1988.com/Spiderman-City-Raid’ (length=39)
‘img’ =>
array (size=2)
0 => string ‘http://www.z1988.com/thumb/12/Spiderman-City-Drive.jpg’ (length=53)
1 => string ‘http://www.z1988.com/thumb/12/Spiderman-City-Raid.jpg’ (length=52)
[/php]

强大的是pq选择器,语法类似jQuery,很方便。

项目下载地址:http://code.google.com/p/phpquery/
github地址:https://github.com/TobiaszCudnik/phpquery

如果你使用过jQuery,你会发现这一切是如此的相象。

如何快速方便的获取到网页的 title?

赞(0) 打赏
未经允许不得转载:程序开发爱好者 » phpQuery—基于jQuery的PHP实现
分享到: 更多 (0)

网站设计及开发、微应用及微网站开发、微信公众平台开发、 HTML5手机网站开发、网络整合营销、APP开发、软件开发及企业电商服务

联系我们