2013-04-28 57 views
1

我有一個小腳本從谷歌獲取關鍵字位置:如何獲取谷歌關鍵字的位置?

if($_POST) { 
//print('post'); 
    // Clean the post data and make usable 
    $domain = filter_var($_POST['domain'], FILTER_SANITIZE_STRING); 
    $keywords = filter_var($_POST['keywords'], FILTER_SANITIZE_STRING); 
     // Remove begining http and trailing/
     $domain = substr($domain, 0, 7) == 'http://' ? substr($domain, 7) : $domain; 
     $domain = substr($domain, -1) == '/' ? substr_replace($domain, '', -1) : $domain; 
     // Replace spaces with + 
     $keywords = strstr($keywords, ' ') ? str_replace(' ', '+', $keywords) : $keywords; 
       $keywords = urlencode($keywords); 

    // Grab the Google page using the chosen keywords 
    $html = new DOMDocument(); 
    @$html->loadHtmlFile('http://www.google.hu/search?q='.$keywords.'&num=100'); 
     print('http://www.google.hu/search?q='.$keywords.'&num=100'); 

    $xpath = new DOMXPath($html); 
    // Store the domains to nodes 
    $nodes = $xpath->query('//div[1]/cite'); 

    // Loop through the nodes to look for our domain 
    $hit = 2; 
    foreach ($nodes as $n){ 
     // echo '<div style="font-size:0.7em">'.$n->nodeValue.'<br /></div>'; // Show all links 
     if (strstr($n->nodeValue, $domain)) { 
      $message = $i; $hit = 1; 
     } 
     else { ++$i; } 
    } 
} 

它幾乎偉大工程。有了一些關鍵詞,它就不會起作用,但是對於其他關鍵詞來說,它的效果非常好。例如:

  • 關鍵字:ingyen társkereső
  • 域:http://ingyen-tarskereso.hu

這是查詢網址:http://www.google.hu/search?q=ingyen%2Btarskereso&num=100

它不工作。但與關鍵字ingyenes fájlmegosztás和域boxy.tigyisolutions.hu,它的工作原理。

你知道什麼是錯的嗎?

也許num100不工作?

回答

1

的解決方案是如此簡單:

只有查詢網址:

http://www.google.com/search?as_q='.$keywords.'&hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial&num=100&start=$start&btnG=Google+Search&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=&as_qdr=all&as_occt=any&as_dt=i&as_sitesearch=&as_rights=&safe=images