设为首页 收藏本站
查看: 676|回复: 0

[经验分享] 利用php代码实现关键字百度排名批量查询

[复制链接]

尚未签到

发表于 2015-8-29 09:15:36 | 显示全部楼层 |阅读模式
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>关键字百度排名批量查</title>
<base target="_blank" />
<meta http-equiv="x-ua-compatible" content="ie=emulateie7" />
<style type="text/css教程">
body{margin:0;padding:0;background:#fff;color:#000;font:12px 微软雅黑, verdana, tahoma, lucida grande, arial, sans-serif;text-align:center;}
#wrapper{width:990px;margin:0 auto;line-height:20px;text-align:left;}
#header,#main,#footer{clear:both;float:left;margin:10px 0 0 0;width:100%;}
a,a:visited{color:#0196e3;text-decoration:none;}
form{margin:10px;}
.kinput{width:300px;margin:0 5px;padding:2px;text-align:left;border:1px solid #ccc;font-weight:bold;}
.kbutton{width:80px;margin:0 5px;padding:2px;height:20px;}
#kgrid{float:left;width:100%;}
.s_kw,.s_rank,.s_wt,.s_title,.s_cache,.s_feng,.s_baidu,.s_mu{float:left;height:30px;line-height:30px;overflow:hidden;text-align:left;border:1px solid #c2d5e3;}
.s_bar{clear:both;float:left;width:100%;height:30px;line-height:30px;text-align:center;}
.s_kw{clear:both;width:120px;}
.s_rank{width:30px;}
.s_wt{width:50px;color:#0e774a;}
.s_title{width:340px;}
.s_cache{width:80px;}
.s_feng{width:80px;}
.s_baidu{width:150px;}
.s_mu{width:120px;}
</style>
</head>

<body>
<div id="wrapper">
<?php教程
function tongji()
{
  $myhost = $_server['http_host'];
  if (preg_match("/(www.)?baiek.com/i", $myhost, $myout))
  {
   echo '<script language="网页特效" type="text/javascript" src="http://js.users.51.la/4295418.js"></script>';
  }
}

function libxml_display_error($error)
{
  $return = "<br/>n";
  switch ($error->level)
  {
   case libxml_err_warning:
    $return .= "<b>warning $error->code</b>: ";
    break;
   case libxml_err_error:
    $return .= "<b>error $error->code</b>: ";
    break;
   case libxml_err_fatal:
    $return .= "<b>fatal error $error->code</b>: ";
    break;
  }
  $return .= trim($error->message);
  if ($error->file)
  {
   $return .= " in <b>$error->file</b>";
  }
  $return .= " on line <b>$error->line</b>n";
  return $return;
}

function libxml_display_errors()
{
  $errors = libxml_get_errors();
  foreach ($errors as $error)
  {
   print libxml_display_error($error);
  }
  libxml_clear_errors();
}

$kfname = '';
if (isset($_get['kf']))
{
  $kfname = trim($_get['kf']);
}
$version = '<script language="javascript" type="text/javascript" src="http://www.zhutiai.com/version.js"></script>';
echo <<< eoth
<div id="header">
  <a href="http://www.3ppt.com/"><b>IT泡泡堂</b></a>
  <a href="http://www.zhutiai.com/" target="_self"><h1>关键字百度排名批量查询(php网页版) baiek.com</h1></a>
  <form method="get" target="_self">
   <input id="kf" name="kf" type="text" class="kinput" value="{$kfname}" />
   <input id="submitbtn" type="submit" class="kbutton" value="批量查询" />
   &emsp;<a href="http://www.zhutiai.com/baiek.rar" target="_self"><b>点击此处下载(更新于{$version})</b></a>
   &emsp;在线演示:<a href="http://www.zhutiai.com/?kf=kw1.xml" target="_self">演示1</a>&emsp;<a href="http://www.zhutiai.com/?kf=kw2.xml" target="_self">演示2</a>&emsp;<a href="http://www.zhutiai.com/?kf=kw3.xml" target="_self">演示3</a>
  </form>
</div>

eoth;
if (!file_exists($kfname) || !is_readable($kfname))
{
  tongji();
  exit('请输入正确的关键字清单文件(xml格式,请参考<a href="http://www.zhutiai.com/readme.txt">readme</a>文件)!!');
}
libxml_use_internal_errors(true);
$doc = new domdocument();
$doc->load($kfname);
if (!$doc->schemavalidate('kw.xsd'))
{
  print '<b>关键字列表xml文件发现错误!</b>';
  libxml_display_errors();
  tongji();
  exit;
}

//由xml文件提取关键字列表与相应的域名
$kwlist = array(); $g = 1;
$kgroups教程 = $doc->getelementsbytagname("kgroup");
foreach ($kgroups as $kgroup)
{
  $kwlist[$g]['domain'] = trim($kgroup->getelementsbytagname("kdomain")->item(0)->nodevalue);
  $kwords = $kgroup->getelementsbytagname("kword");
  $w = 1;
  foreach ($kwords as $kword)
  {
   if ($kword->haschildnodes())
   {
    $kwlist[$g]['kword'][$w] = trim($kword->firstchild->nodevalue);
   }
   $w++;
  }
  $g++;
}
?>
<div id="main">
<?php
function get_dm_weight($h, $i)
{
  //$h表示搜索结果的url,$i表示结果排名
  $p = ceil($i / 10);  //搜索结果第几页
  
  $i_weight = array(1 => 52, 2 => 15, 3 => 10, 4 => 5, 5 => 5, 6 => 4, 7 => 1, 8 => 3, 9 => 2, 0 => 3);
  $h_str = preg_replace("/^.*?://(.*?)(#.*)?$/i", "$1$3", $h); //去除url中的协议(例如http)与#信息片段部分
  $h_arr = explode("?", $h_str, 2);
  $h0_arr = explode("/", $h_arr[0]);
  if ($h0_arr[count($h0_arr) - 1] == '')
  {
   $level_l = count($h0_arr) - 1;
  }
  else
  {
   $level_l = count($h0_arr);
  }
  if (isset($h_arr[1]))
  {
   if ($h_arr[1] != '')
   {
    $level_r = count(explode("&", $h_arr[1]));
   }
   else
   {
    $level_r = 0;
   }
  }
  else
  {
   $level_r = 0;
  }
  $dw = $i_weight[$i % 10] * pow(0.5, ($level_l + $level_r - 1)) * 9 / pow(10, $p);
  return number_format($dw, 1);;
}

function fetch_baidu($d, $k)
{
  $urlw = urlencode(iconv("utf-8","gbk//ignore",$k));
  $max_srh_page = 2; //百度搜索结果50条/页,提取2页,也就是只在前100条搜索结果中检查排名,最大值可以设为16
  $baidu_ids = array(); //存储百度系列子站点占据的排名位置
  $baidu_mus = array(); //存储百度开放平台等优质站点占据的排名位置
  $isrank = 0; //$isrank = 1 当前域名下这个关键词获得排名; $isrank = 0 当前域名下这个关键词没有排名
  $all_count = 0; //测试变量,以确认匹配规则不会遗漏任何一条搜索结果
  $dm_weight = 0; //分析搜索结果页面中顶级、次级、目录、内页的情况,粗略反映一个关键字的竞争激烈程度,非常不准,仅供参考
  for ($page_no = 1; $page_no <= $max_srh_page; $page_no++)
  {
   if ($page_no > 16) break;
   $fail_try = 1;
   $pn = ($page_no - 1) * 50;
   $url = "http://www.baidu.com/s?wd={$urlw}&pn={$pn}&rn=50";
   $snoopy = new snoopy;
//  $snoopy->proxy_host = "127.0.0.1"; //采集可选代理ip,以免频繁抓百度反被百度咬
//  $snoopy->proxy_port = "80";   //proxy代理所用端口
   $snoopy->fetch($url);
   $contents = iconv("gbk","utf-8//ignore",$snoopy->results);
   unset($snoopy);
//  echo $contents;
   if (!preg_match("/<span>此内容系百度根据您的指令自动搜索的结果/i",$contents,$out))
   {
    if ($fail_try > 5)
    {
     continue;
    }
    else
    {
     $fail_try++;
     $page_no--;
     sleep(30);
     continue;
    }
   }
   if (!isset($ebaidu))
   {
    $ebaidu = array('lt' => 0, 'lb' => 0, 'r' => 0); //记录百度推广数量,分为左上、左下、右侧
    if (preg_match_all("/<tr><td class="f ec_pp"><a id="awd+"/i", $contents, $out_lt))
    {
     $ebaidu['lt'] = count($out_lt[0]);
    }
    if (preg_match_all("/<table id="40d+".*?class="ec_mr15">/i", $contents, $out_lt))
    {
     $ebaidu['lt'] = count($out_lt[0]);
    }
    if (preg_match_all("/<table width="65%".*?class="ec_mr15">/i", $contents, $out_lb))
    {
     $ebaidu['lb'] = count($out_lb[0]);
    }
    if (preg_match_all("/<div id="bdfsd+" class="ec_pp".*?><a id=dfsd+/i", $contents, $out_r))
    {
     $ebaidu['r'] = count($out_r[0]);
    }
   }
   if (preg_match_all("/((<table cellpadding="0" cellspacing="0".*?id="(d+)" mu="(.*?)">)|(<table id="(d+)"  cellpadding="0" cellspacing="0" mu="(.*?)">))[s|s]*?((<a.*?href=".*?".*?>(.*?)</a>)|(<div id="app_.*?"></div>))/i", $contents, $out_mu))
   {
    foreach ($out_mu[0] as $om_key => $om_val)
    {
     $om_id1 = $out_mu[3][$om_key];
     $om_href1 = $out_mu[4][$om_key];
     $om_id2 = $out_mu[6][$om_key];
     $om_href2 = $out_mu[7][$om_key];
     $om_title = strip_tags($out_mu[10][$om_key]);
     $baidu_mus[] = $om_id1 . $om_id2;
     $dm_weight = $dm_weight + get_dm_weight($om_href1 . $om_href2, $om_id1 . $om_id2);
     if (preg_match("/://(w*?.)*?baidu.com//i", $om_href1 . $om_href2, $om_domain))
     {
      $baidu_ids[] = $om_id1 . $om_id2;
     }
     if (preg_match("/://(w*?.)*?{$d}//i", $om_href1 . $om_href2, $om_domain))
     {
      echo '<div></div><span class="s_kw"><a href="http://www.baidu.com/s?wd=' . $urlw . '">' . $k . '</a></span><span class="s_rank">' . $om_id1 . $om_id2 . '</span><span class="s_wt"></span><span class="s_title"><a href="' . $om_href1 . $om_href2 . '">' . $om_title . '</a></span><span class="s_cache"></span>';
      $isrank = 1;
     }
    }
   }
   if (preg_match_all("/<table cellpadding="0" cellspacing="0" class="result" id="(d+)"><tr><td class="f"><h3 class="t">(<font.*?</font>)?<a.*?href="(.*?)" target="_blank">(.*?)</a></h3><font size=-1>.*?<span class="g">.*? ((d{4}-d{1,2}-d{1,2})|(d+小时前)|(d+分钟前)) .*?</span>.*?<br></font></td></tr></table>/i", $contents, $out_all))
   {
    foreach ($out_all[0] as $o_key => $o_val)
    {
     $o_id = $out_all[1][$o_key];
     $o_href = $out_all[3][$o_key];
     $o_title = strip_tags($out_all[4][$o_key]);
     $o_cache = $out_all[6][$o_key] . $out_all[7][$o_key] . $out_all[8][$o_key];
     $dm_weight = $dm_weight + get_dm_weight($o_href, $o_id);
     if (preg_match("/://(w*?.)*?baidu.com//i", $o_href, $o_domain))
     {
      $baidu_ids[] = $o_id;
     }
     if (preg_match("/://(w*?.)*?{$d}//i", $o_href, $o_domain))
     {
      echo '<div></div><span class="s_kw"><a href="http://www.baidu.com/s?wd=' . $urlw . '">' . $k . '</a></span><span class="s_rank">' . $o_id . '</span><span class="s_wt">' . $dm_weight . '%</span><span class="s_title"><a href="' . $o_href . '">' . $o_title . '</a></span><span class="s_cache">' . $o_cache . '</span>';
      $isrank = 1;
     }
    }
   }
   //$all_count用来检查上述正则匹配是否匹配到所有搜索结果,特别关注百度系列站点、百度开放平台以及百度应用等有别于普通搜索结果
   if (isset($out_mu[0])) $all_count = $all_count + count($out_mu[0]);
   if (isset($out_all[0])) $all_count = $all_count + count($out_all[0]);
//  echo '<br />总共找到' . $all_count . '个匹配<br />';
   if (!preg_match("/<a href="s?wd=.*?>下一页</a>.*?</p>/i",$contents,$out))
   {
    break;
   }
  }
  if (count($baidu_mus) >= 1)
  {
   $bmus = implode(",", $baidu_mus);
  }
  else
  {
   $bmus = '';
  }
  if (count($baidu_ids) >= 1)
  {
   $bids = implode(",", $baidu_ids);
  }
  else
  {
   $bids = '';
  }
  if ($isrank == 1)
  {
   echo '<span class="s_feng">上' . $ebaidu['lt'] . '下' . $ebaidu['lb'] . '右' . $ebaidu['r'] . '</span><span class="s_baidu">' . $bids . '</span><span class="s_mu">' . $bmus . '</span>';
  }
  else
  {
   echo '<div></div><span class="s_kw"><a href="http://www.baidu.com/s?wd=' . $urlw . '">' . $k . '</a></span><span class="s_rank">0</span><span class="s_wt">' . $dm_weight . '%</span><span class="s_title"></span><span class="s_cache"></span><span class="s_feng">上' . $ebaidu['lt'] . '下' . $ebaidu['lb'] . '右' . $ebaidu['r'] . '</span><span class="s_baidu">' . $bids . '</span><span class="s_mu">' . $bmus . '</span>';
  }
  unset($ebaidu);
}
//逐个域名与关键字采集百度排名信息并显示
set_include_path(".");
include "snoopy.class.php";
set_time_limit(0);
ob_flush(); flush(); ob_end_clean(); ob_implicit_flush(1);
echo '<div id="kgrid">';
echo '<span class="s_kw">关键字</span><span class="s_rank">排名</span><span class="s_wt">竞争度</span><span class="s_title">网页标题</span><span class="s_cache">百度快照</span><span class="s_feng">百度推广数量</span><span class="s_baidu">百度占位</span><span class="s_mu">百度mu占位</span>';
foreach ($kwlist as $d_ks)
{
  $dm = $d_ks['domain'];
  echo '<div></div><span class="s_bar">域名:' . $dm . '</span>';
  foreach ($d_ks['kword'] as $k)
  {
   fetch_baidu($dm, $k);
  }
}
echo '</div>';
?>
</div><!--div main end-->
</div><!--div wrap end-->
<?php
tongji();
?>
</body>
</html>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-105758-1-1.html 上篇帖子: mediawiki中localsettings.php的配置方法 下篇帖子: php 在各种web服务器的运行模式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表