Php keyword Baidu ranking batch query

Source: Internet
Author: User
Tags explode keyword list php tutorial pow wrapper


<! 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> keyword Baidu ranking batch query </title>
<Base target = "_ blank"/>
<Meta http-equiv = "x-ua-compatible" content = "ie = emulateie7"/>
<Style type = "text/css tutorial">
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 Tutorial
Function tongji ()
{
$ Myhost = $ _ server ['http _ host'];
If (preg_match ("/(www .)? Baiek.com/I ", $ myhost, $ myout ))
 {
Echo '<script language = "webpage effect" type = "text/javascript" src ="'"> Http://js.users.51.la/4295418.js">';
 }
}

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.111cn.net/version.js">';
Echo <eoth
<Div id = "header">
If you are a friend of an enterprise site or a friend of an enterprise site, please support my small site <a href ="198114 "> http://www.198114.com/">198114 Product Enterprise category directory </B> </a>. Thank you !!
<A href = "http://www.111cn.net/" target = "_ self"> <Form method = "get" target = "_ self">
<Input id = "kf" name = "kf" type = "text" class = "kinput" value = "{$ kfname}"/>
<Input id = "submitbtn" type = "submit" class = "kbutton" value = "batch query"/>
& Emsp; <a href = "http://www.111cn.net/baiek.rar" target = "_ self"> <B> Click here to download (updated on {$ version}) </B> </a>
& Emsp; online demo: <a href = "http://www.111cn.net /? Kf = kw1.xml "target =" _ self "> Demo 1 </a> & emsp; <a href =" http://www.111cn.net /? Kf = kw2.xml "target =" _ self "> Demo 2 </a> & emsp; <a href =" http://www.111cn.net /? Kf = kw3.xml "target =" _ self "> demonstration 3 </a>
</Form>
</Div>

Eoth;
If (! File_exists ($ kfname) |! Is_readable ($ kfname ))
{
Tongji ();
Exit ('Enter the correct keyword listing file (xml format, see The <a href = "http://www.111cn.net/readme.txt"> readme </a> File )!! ');
}
Libxml_use_internal_errors (true );
$ Doc = new domdocument ();
$ Doc-> load ($ kfname );
If (! $ Doc-> schemavalidate ('KW. xsd '))
{
Print '<B> keyword list xml file found error! </B> ';
Libxml_display_errors ();
Tongji ();
Exit;
}

// Extract the keyword list and corresponding domain name from the xml file
$ Kwlist = array (); $ g = 1;
$ Kgroups tutorial = $ 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 indicates the url of the search result, and $ I indicates the result ranking.
$ P = ceil ($ I/10); // page of the search result
 
$ 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); // remove the protocol (for example, http) and # segment in the url
$ 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; // Baidu search results 50/page, extract 2 pages, that is, only check the ranking of the first 100 results, the maximum value can be set to 16
$ Baidu_ids = array (); // stores the ranking of Baidu sub-sites
$ Baidu_mus = array (); // stores the ranking of high-quality sites such as Baidu open platform
$ Isrank = 0; // $ isrank = 1 the keyword under the current domain name is ranked; $ isrank = 0 the keyword under the current domain name is not ranked
$ All_count = 0; // test the variable to confirm that the matching rule does not omit any search results
$ Dm_weight = 0; // analyze top-level, secondary, directory, and inner pages on the search result page, which roughly reflects the degree of competition for a keyword and is very inaccurate for reference only
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"; // collect optional proxy ip addresses to avoid frequent Baidu attacks
// $ Snoopy-> proxy_port = "80"; // port used by the proxy
$ Snoopy-> fetch ($ url );
$ Contents = iconv ("gbk", "UTF-8 // ignore", $ snoopy-> results );
Unset ($ snoopy );
// Echo $ contents;
If (! Preg_match ("/<span> This content is the result of Baidu's automatic search based on your instructions/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); // record Baidu promotion quantity, which can be divided into top left, bottom left, and right
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] *? (<.*? 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 style = "clear: both"> </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>

  {
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 style = "clear: both"> </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 is used to check whether the above regular match matches all the search results. It is particularly important to pay attention to the Baidu series sites, Baidu open platform, Baidu applications, and other different general search results.
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/> a total of'. $ all_count. 'matches <br/> ';
If (! Preg_match ("/<a href =" s? Wd =. *?> Next page </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"> Top '. $ ebaidu ['Lt ']. 'under '. $ ebaidu ['lb']. 'right '. $ ebaidu ['R']. '</span> <span class = "s_baidu"> '. $ bids. '</span> <span class = "s_mu"> '. $ bmus. '</span> ';
 }
Else
 {
Echo '<div style = "clear: both"> </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"> Shang'. $ ebaidu ['Lt ']. 'under '. $ ebaidu ['lb']. 'right '. $ ebaidu ['R']. '</span> <span class = "s_baidu"> '. $ bids. '</span> <span class = "s_mu"> '. $ bmus. '</span> ';
 }
Unset ($ ebaidu );
}
// Collect Baidu ranking information and display domain names and keywords one by one
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"> keyword </span> <span class = "s_rank"> ranking </span> <span class = "s_wt"> competition </ span> <span class = "s_title"> Webpage title </span> <span class = "s_cache"> Baidu snapshot </span> <span class = "s_feng"> Baidu promotion quantity </span> <span class = "s_baidu"> Baidu placeholder </span> <span class = "s_mu"> Baidu mu placeholder </span> ';
Foreach ($ kwlist as $ d_ks)
{
$ Dm = $ d_ks ['domain '];
Echo '<div style = "clear: both"> </div> <span class = "s_bar"> domain name:'. $ 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.