phpQuery - PHP 处理 HTML DOM 的好帮手
2014-04-07 17:55
381 查看
phpQuery, 在 PHP 處理 HTML DOM 的好幫手阿!! 哈! 這個 project 真不錯, 尤其是對於有使用 jQuery 的人, 更容易上手阿 :p 顧名思義, 就是 PHP 的 jQuery 阿! DOM select 的語法跟 jQuery 完全一樣阿! 而且, 當然是 chainable :p 也就是說, 只有 PHP 5 能用囉 :p
目前在 stickeraction 有用到 phpQuery, 真的是很方便 :p
可以看看以下範例, 這一段是我用來抓取網頁中, rss feed url 跟 title 的。
php代码
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 發出來的…
目前在 stickeraction 有用到 phpQuery, 真的是很方便 :p
可以看看以下範例, 這一段是我用來抓取網頁中, rss feed url 跟 title 的。
php代码
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 發出來的…
相关文章推荐
- 使用phpQuery轻松采集网页内容
- UINavigationController使用简介
- UITableView删除移动单元格思路
- easyui合并多行问题
- 子线程中循环操作UI线程时导致子线程无法正常结束的问题总结
- iOS开发(10)UISegmentedControl
- GUI(十)
- Priority queue
- 【DS】排序算法之快速排序(Quick Sort)
- iOS开发(9)UISlider
- iOS开发(8)UISwitch
- django中404错误CSRF verification failed. Request aborted
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS开发(7)通过uiimageView浅谈UIView
- ios开发(6)uiimageView
- N-Queens
- Java线程(七):阻塞队列BlockingQueue
- Longest Repeated Sequence
- Longest Repeated Sequence
- ffmpeg 转AAC时出现Unable to find a suitable output format for