怎麼取出百度某熱門排行榜50條並在每條加前尾碼呢

來源:互聯網
上載者:User
如http://top.baidu.com/buzz?b=26&c=1&fr=topcategory_c1


回複討論(解決方案)

已把主要???整合??,加前後?你想怎?加都可以了。

(.*?)<\/td>/is', $content, $keywords);$data = $keywords[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/a>/is', $val, $tmp);array_push($result, array('name'=>$tmp[2][0],'url'=>'http://top.baidu.com'.substr($tmp[1][0],1)));}return $result;}function getTc($content){// tcpreg_match_all('/(.*?)<\/td>/is', $content, $tc);$data = $tc[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/a>/is', $val, $tmp);array_push($result, array('brief'=>$tmp[1][0], 'news'=>$tmp[1][1], 'tieba'=>$tmp[1][2]));}return $result;}function getNum($content){// lastpreg_match_all('/(.*?)<\/td>/is', $content, $last); //icon-rise up icon-fall down$data = $last[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/span>/is', $val, $tmp);array_push($result, array('flag'=>str_replace('icon-','',$tmp[1][0]), 'num'=>$tmp[2][0]));}return $result;}$url = 'http://top.baidu.com/buzz?b=26&c=1&fr=topcategory_c1';$content = getContent($url);$result = array();$keywords = getKeywords($content);$tc = getTc($content);$num = getNum($content);for($i=0,$len=count($keywords); $i<$len; $i++){$tmp = array('name' => $keywords[$i]['name'],'url' => $keywords[$i]['url'],'brief' => $tc[$i]['brief'],'news' => $tc[$i]['news'],'tieba' => $tc[$i]['tieba'],'flag' => $num[$i]['flag'],'num' => $num[$i]['num']);array_push($result, $tmp);}echo '
';print_r($result);echo '
';?>

?集後的??格式如下:

Array(    [0] => Array        (            [name] => 匆匆那年            [url] => http://top.baidu.com/detail?b=26&c=1&w=%B4%D2%B4%D2%C4%C7%C4%EA            [brief] => http://baike.baidu.com/search/word?word=%B4%D2%B4%D2%C4%C7%C4%EA            [news] => http://news.baidu.com/ns?tn=news&from=news&cl=2&rn=20&ct=1&word=%B4%D2%B4%D2%C4%C7%C4%EA            [tieba] => http://tieba.baidu.com/f?kw=%B4%D2%B4%D2%C4%C7%C4%EA            [flag] => rise            [num] => 162540        )    [1] => Array        (            [name] => 一個人的武林            [url] => http://top.baidu.com/detail?b=26&c=1&w=%D2%BB%B8%F6%C8%CB%B5%C4%CE%E4%C1%D6            [brief] => http://baike.baidu.com/search/word?word=%D2%BB%B8%F6%C8%CB%B5%C4%CE%E4%C1%D6            [news] => http://news.baidu.com/ns?tn=news&from=news&cl=2&rn=20&ct=1&word=%D2%BB%B8%F6%C8%CB%B5%C4%CE%E4%C1%D6            [tieba] => http://tieba.baidu.com/f?kw=%D2%BB%B8%F6%C8%CB%B5%C4%CE%E4%C1%D6            [flag] => rise            [num] => 75428        )    [2] => Array        (            [name] => 星際穿越            [url] => http://top.baidu.com/detail?b=26&c=1&w=%D0%C7%BC%CA%B4%A9%D4%BD            [brief] => http://baike.baidu.com/search/word?word=%D0%C7%BC%CA%B4%A9%D4%BD            [news] => http://news.baidu.com/ns?tn=news&from=news&cl=2&rn=20&ct=1&word=%D0%C7%BC%CA%B4%A9%D4%BD            [tieba] => http://tieba.baidu.com/f?kw=%D0%C7%BC%CA%B4%A9%D4%BD            [flag] => rise            [num] => 70538        )    [3] => Array        (            [name] => 心花路放            [url] => http://top.baidu.com/detail?b=26&c=1&w=%D0%C4%BB%A8%C2%B7%B7%C5            [brief] => http://baike.baidu.com/search/word?word=%D0%C4%BB%A8%C2%B7%B7%C5            [news] => http://news.baidu.com/ns?tn=news&from=news&cl=2&rn=20&ct=1&word=%D0%C4%BB%A8%C2%B7%B7%C5            [tieba] => http://tieba.baidu.com/f?kw=%D0%C4%BB%A8%C2%B7%B7%C5            [flag] => fall            [num] => 68233        )        。。。)

function getContent 修改?以下代?。

function getContent($url){$ch = curl_init();curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_TIMEOUT, 300);$response = curl_exec($ch);if($error=curl_error($ch)){die($error);}curl_close($ch);$content = iconv('GB2312','UTF8//IGNORE',$response);return $content;}

已把主要???整合??,加前後?你想怎?加都可以了。

(.*?)<\/td>/is', $content, $keywords);$data = $keywords[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/a>/is', $val, $tmp);array_push($result, array('name'=>$tmp[2][0],'url'=>'http://top.baidu.com'.substr($tmp[1][0],1)));}return $result;}function getTc($content){// tcpreg_match_all('/(.*?)<\/td>/is', $content, $tc);$data = $tc[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/a>/is', $val, $tmp);array_push($result, array('brief'=>$tmp[1][0], 'news'=>$tmp[1][1], 'tieba'=>$tmp[1][2]));}return $result;}function getNum($content){// lastpreg_match_all('/(.*?)<\/td>/is', $content, $last); //icon-rise up icon-fall down$data = $last[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/span>/is', $val, $tmp);array_push($result, array('flag'=>str_replace('icon-','',$tmp[1][0]), 'num'=>$tmp[2][0]));}return $result;}$url = 'http://top.baidu.com/buzz?b=26&c=1&fr=topcategory_c1';$content = getContent($url);$result = array();$keywords = getKeywords($content);$tc = getTc($content);$num = getNum($content);for($i=0,$len=count($keywords); $i<$len; $i++){$tmp = array('name' => $keywords[$i]['name'],'url' => $keywords[$i]['url'],'brief' => $tc[$i]['brief'],'news' => $tc[$i]['news'],'tieba' => $tc[$i]['tieba'],'flag' => $num[$i]['flag'],'num' => $num[$i]['num']);array_push($result, $tmp);}echo '
';print_r($result);echo '
';?>


為什麼我測試時,這個數組輸出的是空的

你的php檔案是否utf8

你的php檔案是否utf8


是的

那你先把getContent的返回?出,看看有什?。

?了,你把????打?,如果白屏估?是??。你是否有安?curl?
在?面?加上
ini_set('display_errors','on');
error_reporting(E_ALL);
看看有什??。如果提示curl?有安?的,
可以改getContent?

function getContent($url){    $response = file_get_contents($url); // ?句代替curl     $content = iconv('GB2312','UTF8//IGNORE',$response);     return $content;}

什麼都沒輸出
編碼換成ANSI也不成

你測試可用的檔案能打包的嗎

Notice: iconv() [function.iconv]: Wrong charset, conversion from `GB2312' to `UTF8//IGNORE' is not allowed in F:\PHPnow-1.5.6\vhosts\test.php on line 15

$content = iconv('GB2312','UTF-8',$response);


這樣就成了


問題我是個菜鳥,這個數組要怎麼取出來正常使用呢

$content = iconv('GBK','UTF-8//IGNORE', $response);

Notice: iconv() [function.iconv]: Wrong charset, conversion from `GB2312' to `UTF8//IGNORE' is not allowed in F:\PHPnow-1.5.6\vhosts\test.php on line 15

$content = iconv('GB2312','UTF8//IGNORE',$response);
改?
$content = iconv('GBK','UTF8//IGNORE',$response);
??

拿到???不知道怎?用。。。呵呵。

你想?示的?子是怎?的,就做成怎?的html,然後在需要的地方,插入????。

數組一巧不同,鬱悶了

其實我只要成這樣就行的

匆匆那年匆匆那年匆匆那年匆匆那年匆匆那年

??出的完整例子,你好好??下。
中?你想改什?,直接改就可以了。

(.*?)<\/td>/is', $content, $keywords);$data = $keywords[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/a>/is', $val, $tmp);array_push($result, array('name'=>$tmp[2][0],'url'=>'http://top.baidu.com'.substr($tmp[1][0],1)));}return $result;}function getTc($content){// tcpreg_match_all('/(.*?)<\/td>/is', $content, $tc);$data = $tc[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/a>/is', $val, $tmp);array_push($result, array('brief'=>$tmp[1][0], 'news'=>$tmp[1][1], 'tieba'=>$tmp[1][2]));}return $result;}function getNum($content){// lastpreg_match_all('/(.*?)<\/td>/is', $content, $last); //icon-rise up icon-fall down$data = $last[1];$result = array();foreach($data as $val){preg_match_all('/(.*?)<\/span>/is', $val, $tmp);array_push($result, array('flag'=>str_replace('icon-','',$tmp[1][0]), 'num'=>$tmp[2][0]));}return $result;}$url = 'http://top.baidu.com/buzz?b=340&c=1&fr=topbuzz_b339_c1';$content = getContent($url);$result = array();$keywords = getKeywords($content);$tc = getTc($content);$num = getNum($content);for($i=0,$len=count($keywords); $i<$len; $i++){$tmp = array('name' => $keywords[$i]['name'],'url' => $keywords[$i]['url'],'brief' => $tc[$i]['brief'],'news' => $tc[$i]['news'],'tieba' => $tc[$i]['tieba'],'flag' => $num[$i]['flag'],'num' => $num[$i]['num']);array_push($result, $tmp);}?>      baidu   
">">?介">新?">?吧
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.