怎么取出百度某排行榜50条并在每条加前后缀呢 百度前后缀 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 已把主要數據採集成數組,加前後綴你想怎樣加都可以了。<?phpfunction getContent($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); $response = curl_exec($ch); if($error=curl_error($ch)){ die($error); } curl_close($ch); $content = iconv('GB2312','UTF8',$response); return $content;}function getKeywords($content){ // keywords preg_match_all('/<td class="keyword">(.*?)<\/td>/is', $content, $keywords); $data = $keywords[1]; $result = array(); foreach($data as $val){ preg_match_all('/<a.*?href="(.*?)".*?>(.*?)<\/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){ // tc preg_match_all('/<td class="tc">(.*?)<\/td>/is', $content, $tc); $data = $tc[1]; $result = array(); foreach($data as $val){ preg_match_all('/<a.*?href="(.*?)".*?>(.*?)<\/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){ // last preg_match_all('/<td class="last">(.*?)<\/td>/is', $content, $last); //icon-rise up icon-fall down $data = $last[1]; $result = array(); foreach($data as $val){ preg_match_all('/<span.*?class="(.*?)">(.*?)<\/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 '<pre>';print_r($result);echo '</pre>';?> 採集后的數組格式如下: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;} 那你先把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); $content = iconv('GB2312','UTF8//IGNORE',$response);改為$content = iconv('GBK','UTF8//IGNORE',$response);試試 拿到數組還不知道怎麼用呵呵。你想顯示的樣子是怎樣的,就做成怎樣的html,然後在需要的地方,插入數組數據。 数组一巧不同,郁闷了其实我只要成这样就行的<a href="xxx.php?title=匆匆那年">匆匆那年</a><a href="xxx.php?title=匆匆那年">匆匆那年</a><a href="xxx.php?title=匆匆那年">匆匆那年</a><a href="xxx.php?title=匆匆那年">匆匆那年</a><a href="xxx.php?title=匆匆那年">匆匆那年</a> 帶輸出的完整例子,你好好學習下。中間你想改什麼,直接改就可以了。<?phpfunction 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('GBK','UTF-8//IGNORE',$response); return $content;}function getKeywords($content){ // keywords preg_match_all('/<td class="keyword">(.*?)<\/td>/is', $content, $keywords); $data = $keywords[1]; $result = array(); foreach($data as $val){ preg_match_all('/<a.*?href="(.*?)".*?>(.*?)<\/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){ // tc preg_match_all('/<td class="tc">(.*?)<\/td>/is', $content, $tc); $data = $tc[1]; $result = array(); foreach($data as $val){ preg_match_all('/<a.*?href="(.*?)".*?>(.*?)<\/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){ // last preg_match_all('/<td class="last">(.*?)<\/td>/is', $content, $last); //icon-rise up icon-fall down $data = $last[1]; $result = array(); foreach($data as $val){ preg_match_all('/<span.*?class="(.*?)">(.*?)<\/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);}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title> baidu </title> </head> <body> <table> <?php $i = 1; foreach($result as $val){ ?> <tr> <td><?=$i ?></td> <td><a href="<?=$val['url'] ?>"><?=$val['name'] ?></a></td> <td><a href="<?=$val['brief'] ?>">簡介</a></td> <td><a href="<?=$val['news'] ?>">新聞</a></td> <td><a href="<?=$val['tieba'] ?>">貼吧</a></td> <td><?php if($val['flag']=='fall'){ echo '↓'; }else{echo '↑'; } ?><?=$val['num'] ?></td> </tr> <?php $i++; } ?> </table> </body></html> PHP中Imagick的使用? 关于PHP的几个基础问题 curl遇到的难题 用Smarty生成 静态文件的问题? 请问如何导出csv格式的文件?? PHP的fopen函数是不是只能打开DocumentRoot路径内的文件?还是可以打开任何对apache开放读写权限的文件? gd输出问题 我需要用PHP做一个录入系统,各位请给点意见: php调用Oracle存储存过程如何返回记录集/游标呢? 文件被传到何处? 关于shell的一个问题求大神解决 请教mysql存储过程执行成功,php却取不到值
function getContent($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
if($error=curl_error($ch)){
die($error);
}
curl_close($ch); $content = iconv('GB2312','UTF8',$response);
return $content;
}function getKeywords($content){
// keywords
preg_match_all('/<td class="keyword">(.*?)<\/td>/is', $content, $keywords);
$data = $keywords[1]; $result = array(); foreach($data as $val){
preg_match_all('/<a.*?href="(.*?)".*?>(.*?)<\/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){
// tc
preg_match_all('/<td class="tc">(.*?)<\/td>/is', $content, $tc); $data = $tc[1]; $result = array(); foreach($data as $val){
preg_match_all('/<a.*?href="(.*?)".*?>(.*?)<\/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){
// last
preg_match_all('/<td class="last">(.*?)<\/td>/is', $content, $last); //icon-rise up icon-fall down $data = $last[1]; $result = array(); foreach($data as $val){
preg_match_all('/<span.*?class="(.*?)">(.*?)<\/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 '<pre>';
print_r($result);
echo '</pre>';?>
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
)
)
$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;
}
在頁面頂加上
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也不成你测试可用的文件能打包的吗
问题我是个菜鸟,这个数组要怎么取出来正常使用呢
改為
$content = iconv('GBK','UTF8//IGNORE',$response);
試試
<a href="xxx.php?title=匆匆那年">匆匆那年</a>
<a href="xxx.php?title=匆匆那年">匆匆那年</a>
<a href="xxx.php?title=匆匆那年">匆匆那年</a>
<a href="xxx.php?title=匆匆那年">匆匆那年</a>
<a href="xxx.php?title=匆匆那年">匆匆那年</a>
中間你想改什麼,直接改就可以了。
<?php
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('GBK','UTF-8//IGNORE',$response); return $content;
}function getKeywords($content){
// keywords
preg_match_all('/<td class="keyword">(.*?)<\/td>/is', $content, $keywords);
$data = $keywords[1]; $result = array(); foreach($data as $val){
preg_match_all('/<a.*?href="(.*?)".*?>(.*?)<\/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){
// tc
preg_match_all('/<td class="tc">(.*?)<\/td>/is', $content, $tc); $data = $tc[1]; $result = array(); foreach($data as $val){
preg_match_all('/<a.*?href="(.*?)".*?>(.*?)<\/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){
// last
preg_match_all('/<td class="last">(.*?)<\/td>/is', $content, $last); //icon-rise up icon-fall down $data = $last[1]; $result = array(); foreach($data as $val){
preg_match_all('/<span.*?class="(.*?)">(.*?)<\/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);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title> baidu </title>
</head> <body>
<table>
<?php
$i = 1;
foreach($result as $val){
?>
<tr>
<td><?=$i ?></td>
<td><a href="<?=$val['url'] ?>"><?=$val['name'] ?></a></td>
<td><a href="<?=$val['brief'] ?>">簡介</a></td>
<td><a href="<?=$val['news'] ?>">新聞</a></td>
<td><a href="<?=$val['tieba'] ?>">貼吧</a></td>
<td><?php if($val['flag']=='fall'){ echo '↓'; }else{echo '↑'; } ?><?=$val['num'] ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>