phpQuery, 在 PHP 處理 HTML DOM 的好幫手阿!! 哈! 這個 project 真不錯, 尤其是對於有使用 jQuery 的人, 更容易上手阿 :p 顧名思義, 就是 PHP 的 jQuery 阿! DOM select 的語法跟 jQuery 完全一樣阿! 而且, 當然是 chainable :p 也就是說, 只有 PHP 5 能用囉 :p
目前在 stickeraction 有用到 phpQuery, 真的是很方便 :p
可以看看以下範例, 這一段是我用來抓取網頁中, rss feed url 跟 title 的。
require 'phpQuery.php';
$url = 'http://tzangms.com/blog';
$html = file_get_contents($url);
phpQuery::newDocument($html)->find('head');
$title = pq('head > title')->text();
$feed_url = pq('head > link[rel=alternate]:first')->attr('href');
echo $title;
echo $feed_url;
本來這個工作是直接用 simplepie 來作, 直接就可以抓到 RSS feed url 跟 RSS , 可是在抓 xuite blog 的時候卻有問題, 都抓不到 feed_url。
看了一下 xuite 的 html, 裡面有個 wptpass 這個不知名的 tag, 有人可以告訴我這是啥嗎? anyway, 總之~ 可能就是因為 xuite 的 html code 有點奇怪, 導致 simplepie 沒辦法找到 RSS feed url, 所以我才用了 phpQuery 來找 RSS feed url。
PS. 雖然在抓 xuite html 時 log 中會有一堆錯誤 … 會說 wptpass 不是合法的 tag 之類的… 不過那都是 DOMDocument 發出來的…



我到現在還是用 RegExp 在抓這些資料 (汗)
@CornGuo
regexp 好像比較快ㄝ, 其實 XD
[...] [php] phpQuery 處理HTML DOM Posted in September 23rd, 2008 by admin in OS http://tzangms.com/blog/php/1555 [...]
Hi,
Dont understand your language, google translate doesnt help much ether, but i see your code ;)
Since 0.9.4 you dont have to do file_get_contents any more. You can just use Ajax like in jQuery – phpQuery::get($url, $callback);.
Check release notes:
http://phpquery-library.blogspot.com/2008/09/phpquery-094-released.html
@Tobiasz Cudnik
Wow! this new function is cool! thanx :D
[...] ruby 有沒有處理 HTML DOM 的東西, 就找到了 Hpricot, 其實跟之前用的 phpQuery 差不多, 都可以用 jQuery 的語法來處理 DOM, 哈! [...]
[...] http://tzangms.com/blog/php/1555 我照這個吃下來的Q_Q,可是wp好像會出亂碼… Q____Q… (努力咬文件) [...]
[...] phpQuery – PHP 處理 HTML DOM 的好幫手阿! [...]