教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP 获取文章在百度排名的实例代码

PHP 获取文章在百度排名的实例代码

发布时间:2017-05-03   编辑:jiaochengji.com
本文分享一例php代码,用于取得指定的文章在百度搜索中的排名,感兴趣的朋友参考下,希望对大家有所帮助。

本节内容:
PHP代码获取文章在百度排名

例子:
 

复制代码 代码示例:

<?php
/**
 * $key 百度搜索关键词
 * $url 要查找的文章的URL(可把标题传进来)
 * $max 从百度搜索结果的前多少条查找
 *
 * return $rank
 *
 * @site www.jbxue.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=\"([^\"]+?)\"[^>]*>[\s\S]*?<\/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", "/找到相关网页约[\s\S]*?篇/");
    preg_match($preg, $ct, $match);

    $str = iconv("GB2312","UTF-8",$match[0]);

    return $str;
}

//调用示例 取得百度排名
$res = baidu_total('"脚本学堂"');
print_r($res);
?>

您可能感兴趣的文章:
PHP 获取文章在百度排名的实例代码
php百度天气小偷实现代码
百度、谷歌关键词排名在线查询
php 百度快照、百度收录、百度热词的代码分享
php 获取百度收录和百度快照时间的代码
php实现获取百度收录与快照代码
PHP 获取taobao与百度搜索下拉框内容
PHP调用百度天气接口API实现查询实时天气
解析行业长尾关键词的排名因素
PHP获取域名的几个全局变量

[关闭]
~ ~