该小工具主要用于关键字查询,通过输入自己的网址可以看到自己的关键字在百度的排名情况
首先,我们需要使用到PHP的爬虫queryList
然后下载包。因为我本地是5.3版本,所以我用的是v3。
包加载完了之后,我们就直接上代码了:
function linkUrl($url){
$info = parse_url($url);
@$fp = fsockopen($info['host'], 80,$errno, $errstr, 30);
@fputs($fp,"GET {$info['path']}?{$info['query']} HTTP/1.1\r\n");
@fputs($fp, "Host: {$info['host']}\r\n");
@fputs($fp, "Connection: close\r\n\r\n");
$rewrite = '';
while(!feof($fp)) {
$line = fgets($fp);
if($line != "\r\n" ) {
if(strpos($line,'Location:') !== false) {
$rewrite = str_replace(array("\r","\n","Location: "),'',$line);
}
}else {
break;
}
}
return $rewrite;
}
function domain($url){
preg_match('/[\w][\w-]*\.(?:com\.cn|com|cn|co|net|org|gov|cc|biz|info)(\/|$)/isU', $url, $domain);
return @$domain[0];
}
require 'vendor/phpQuery.php';
require 'vendor/QueryList.php';
use QL\QueryList;
function checkDomain()
{
$post = $_POST;
if (!empty($_POST)) {
for ($b = 0; $b <= 10; $b++) {
$url = 'http://www.baidu.com/s?wd=' . urlencode($post['keyword']) . '&pn=' . $b * 10;
$data = QueryList::Query($url, array("url" => array('h3 a', 'href'), "title" => array('h3', 'text')))->data;//数组
foreach ($data as $k=>$v) {
$v['url'] = domain(linkUrl($v['url']));
if (in_array(domain($post['domain']), $v)) {
$num = $b*10 + $k+1;
$str = array(
'url'=>'目标主域为:' . $v['url'],
'keyword'=>'关键字为:' . $post['keyword'],
'rand'=>'排名为:' . $num,
);
return $str;
}
}
}
die;
}
}
$checkEnd = checkDomain();
if (!empty($_POST)){
if(!empty($checkEnd)){
echo $checkEnd['url'];
echo "</br>";
echo $checkEnd['keyword'];
echo "</br>";
echo $checkEnd['rand'];die;
}else{
echo '关键字的前100没有发现目标网址';die;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>百度排名查询</title>
<style>
html, body, h1, form, fieldset, legend, ol ,li{
padding:0;
margin:0;
}
ol{
list-style:none;
}
body{
background:#fff;
color:#111;
padding:20px;
}
form#payment{
background:#9cbc2c;
-webkit-border-radius:5px;
border-radius:5px;
padding:20px;
width:400px;
}
form#payment fieldset{
border:none;
margin-bottom:10px;
}
form#payment fieldset:last-of-type{ margin-bottom:0; }
form#payment legend{
color:#384313;
font-size:16px;
font-weight:bold;
padding-bottom:10;
text-shadow:0px 1px 1px #c0d576;
}
form#payment > fieldset>legend:before{
content:"Step" counter(fieldset)":";
counter-increment:fieldsets;
}
form#payment fieldset fieldset legend{
color:#111;
font-size:13px;
font-weight:normal;
padding-bottom:0;
}
form#payment ol li{
background:#b9cf6a;
background:rgba(255, 255, 255, 0.3);
border:#e3ebc3;
border-color:rgba(255, 255, 255, 0.6);
border-style:solid;
border-width:2px;
-webkit-border-radius:5px;
line-height:30px;
padding:5px 10px;
margin-bottom:2px;
}
form#payment ol ol li{
bakcground:none;
border:none;
float:left;
}
form#payment label{
float:left;
font-size:13px;
width:110px;
}
form#payment fieldset fieldset label{
background:none no-repeat left 50%;
line-height:20px;
padding:0 0 0 30px;
width:auto;
}
form#payment fieldset fieldset label:hover{cursor:pointer;}
form#payment input:not([type=radio]), form#payment textarea{
background:#fff;
border:#fc3 solid 1px;
-webkit-border-radius:3px;
outline:none;
padding:5px;
}
</style>
</head>
<body>
<form id=payment action="" method="POST">
<fieldset>
<legend>百度前100排名查询</legend>
<ol>
<li>
<label for="name">关键字:</label>
<input type="text" id="name" name="keyword" placeholder="请输入需要查询的关键字" required autofocus>
</li>
<li>
<label for="phone">网址:</label>
<input type="tel" placeholder="网址结尾加/" id="phone" name="domain">
</li>
</ol>
<ol>
<li>
<legend style="color: red">说明:该小功能仅针对百度前110页,且仅能匹配出主域</legend>
</li>
</ol>
</fieldset>
<fieldset>
<button type="submit">提交</button>
</fieldset>
</form>
</body>
</html>
有些报错我没有调整,直接抑制的。
同时正则也是在百度查的,有屌大的童鞋做了优化,可以联系我修改下