本节内容:
PHP代码获取文章在百度排名
例子:
<?php
/**
* $key 百度搜索关键词
* $url 要查找的文章的URL(可把标题传进来)
* $max 从百度搜索结果的前多少条查找
*
* return $rank
*
* @site www.jb200.com
*/
function get_con($key='', $url='', $max=100, $pn=0){
if(!$pn) $key = iconv("UTF-8","GB2312",$key);
$str = "http://www.baidu.com/s?wd=".urlencode($key).'&pn='.$pn;
$str = file_get_contents($str);
preg_match_all("/<table[^>]+id="([^"]+?)"[^>]*>[sS]*?</table>/",$str,$match);
foreach ($match[0] as $key => $val){
if(strstr($val, $url)) {
$rank = $match[1][$key];
break;
}
}
if($rank) {
return $rank;exit;
}else{
$pn += 10;
if($pn > $max) {
return 0;exit;
}
$rank = get_con($key, $url, $max, $pn);
}
return $rank;
}
$res = get_con('中国足球', 'http://zhidao.baidu.com/question/205692751.html?si=10&wtp=wk');
print_r($res);
//相关记录条数
function baidu_total($key='') {
$key = iconv("UTF-8","GB2312",$key);
$str = "http://www.baidu.com/s?wd=".urlencode($key);
$ct = file_get_contents($str);
$preg = iconv("UTF-8", "GB2312", "/找到相关网页约[sS]*?篇/");
preg_match($preg, $ct, $match);
$str = iconv("GB2312","UTF-8",$match[0]);
return $str;
}
//调用示例 取得百度排名
$res = baidu_total('"脚本学堂"');
print_r($res);
?>