如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
<?php header('Content-type:text/html;charset=utf-8'); error_reporting(E_ERROR | E_WARNING | E_PARSE); set_time_limit(0);//d等待时间.不限制 ini_set('memory_limit','200m');//设置内存 $url ='https://s.weibo.com/top/summary?cate=realtimehot'; //获取网页 $str = get_str($url); $block_rule ='/<td class="td-01 ranktop">(.*?)<td class="td-03">/si'; preg_match_all($block_rule,$str,$fenlei); if(!empty($fenlei)){ $count = count($fenlei[1]); for($i=0;$i<$count;$i++){ $cat_rule='/<a href="(.*?)" target="_blank">(.*?)<\/a>/si'; preg_match_all($cat_rule,$fenlei[1][$i],$cats); if(!empty($cats)){ $url_r = $cats[1][0]; $name_r = $cats[2][0]; echo '<a href=https://s.weibo.com'.$url_r.' target="_blank">'.$name_r.'</a><br> '; } } } //curl获取网页内容 function get_str($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//超时 0不限制 //ssl curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $str = curl_exec($ch); curl_close($ch); return $str; }
20181009测试有效