【发布时间】:2017-07-09 18:06:06
【问题描述】:
我有一个 PHP 网络爬虫,它工作得非常好(目前)
它从站点中提取论坛问题及其链接并将其粘贴到我的站点中。
所以,我一直试图让它做同样的事情,除了这次,我希望它从提取站点跳过 2 行。 所以不是从网站获取所有语句,而是从语句 3 开始。
我的代码如下:
<?php
function get_data($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_data('http://www.usmle-forums.com/usmle-step-1-forum/');
$first_step = explode( '<tbody id="threadbits_forum_26"' , $returned_content );
$second_step = explode('</tbody>', $first_step[1]);
$third_step = explode('<tr>', $second_step[0]);
// print_r($third_step);
foreach ($third_step as $key=>$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>";
echo '<li target="_blank" class="itemtitle">';
if($key < 5 && $key > 2 && rand(0,1) == 1) {
echo '<span class="item_new">new</span>';
}
echo $final;
echo '</li>';
if($key==10) {
break;
}
}
?>
任何帮助表示赞赏..
【问题讨论】:
标签: php web-scraping html-parsing limit