phpQuery - PHP 处理 HTML DOM 的好帮手(截取网页特定内容)
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 發出來的…
转载自http://hi.baidu.com/qfans/blog/item/8228d21bd7c11dddad6e7510.html