この素晴らしいWebクローラーコードがあります。それは、関連するリンクと共に、上記のサイトとペーストから要求されたデータを取得します。 (グッドボーイ)Web Crawlerから行を抽出することを制限しました。
問題は、抽出されたデータを5行に制限する方法です。 次のように私のコードが行く::
<div class="news-entry">
<div class="newsblock">
<div style="clear:both"></div>
<h2>
<a rel="nofollow" target="_blank" href="http://www.usmle-forums.com/usmle-step-3-forum/">
USMLE-Forums :: STEP-3
</a>
</h2>
<ul>
<?php
function get_datafour($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
return $result;
}
$returned_content = get_datafour('http://www.usmle-forums.com/usmle-step-3-forum/');
$first_step = explode('<tbody id="threadbits_forum_30">' , $returned_content);
$second_step = explode('</tbody>', $first_step[1]);
$third_step = explode('<tr>', $second_step[0]);
// print_r($third_step);
foreach ($third_step as $element) {
$child_first = explode('<td class="alt1"' , $element);
$child_second = explode('</td>' , $child_first[1]);
$child_third = explode('<a href=' , $child_second[0]);
$child_fourth = explode('</a>' , $child_third[1]);
$final = "<a href=".$child_fourth[0]."</a></br>";
?>
<li target="_blank" class="itemtitle">
<span class="item_new"></span><?php echo $final?>
</li>
<?php
}
?>
</ul>
<div style="clear:both"></div>
</div>
</div>
任意の提案が高く評価され
..私は(私たちは通常、PHPのSQLクエリで行うもの)「LIMIT 5」を入れてみましたが、それはうまくいきませんでした。..
foreachループで5回反復したあと、 –
とそれを行う方法は? – harishk
答えをManvir singh –