Re: [請益] php 抓 google 搜尋結果特定區塊
※ 引述《upp818 (KT)》之銘言:
: 那應該要取 <div> ~~ </div> 還是 <li > ~~ </li> 呢?
你要取的是 <li> 。
: 而在取出某段 html 時,也遇到了一點問題 @@
不用這麼辛苦的用 regular expression,有現成的可以用:
http://simplehtmldom.sourceforge.net/index.htm
API 參考文件: http://simplehtmldom.sourceforge.net/manual_api.htm
範例:
<?php
require_once('simple_html_dom.php');
$html = file_get_html('http://www.google.com.tw/search?q=php');
$li_arr = $html->find('.g');
foreach($li_arr as $li) {
$link = $li->children(0)->children(0)->getAttribute('href');
$text = strip_tags($li->children(0)->children(0)->innertext);
echo "<a href='$link'>$text</a><br />";
}
?>
--
╓╥╥╖╓─╥╖ ╓─╥╖ ╓ ╓╖ ╓─╥╖ ╓─╥╖ ╓╖╓╖
╟╢ ║ ╟╢ ║ ╙╜ ║ ╟╢ ║ ╟╢ ║ ╟╢ ║║╟╢
╟╢ ║ ╟╢ ╟─ ╟─╫╢ ║ ╟╢ ╟─╫╜ ║║╟╢
╟╢ ║ ╟╢ ║ ╓╖ ║ ╟╢ ║ ╟╢ ║ ║ ║║╟╢
╙╨╜ ╙─╨╜ ╙─╨╜ ╙ ╙╜ ╙─╨╜ ╙ ╙╜ ╙╙╨╜
獅子男
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.37.131.225
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):
PHP 近期熱門文章
PTT數位生活區 即時熱門文章