我一直在尝试在我的网络搜寻器提取的语句中添加提到的"文本"。
php web crawler ::
<?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>";
?>
<li target="_blank" class="itemtitle">
<span class="item_new">text</span><?php echo $final?>
</li>
<?php
if($key==10){
break;
}
}
?>
现在,我一直在尝试插入SPAN标签中提到的"文本",以随机添加在Web搜寻器提取的前五个语句中。
因此,每当用户刷新页面时,跨度标签中的"文本"都应在前5个语句之间进行随机供电。
任何帮助都将受到赞赏。
不确定这是否是您想要的:
<?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 && rand(0,1) == 1) {
echo '<span class="item_new">YOUR TEXT </span>';
}
echo $final;
echo '</li>';
if($key==10) {
break;
}
}
?>
它将"您的文本"随机添加到前五行。
输出:
YOUR TEXT
1 Attachment(s)
Pass Your Step 1 Exam - 100% Guaranteed!
USMLE Step 1 Live prep/Boot Camp in Chicago
YOUR TEXT 1 Attachment(s)
Picmonic for Medicine
Increase Your Step 1 Score Through 1-on-1 Online Tutoring
Low Score on Step 1?
step 1 tutoring
Frustrated USMLE taker mom.
Looking for a tutor