如何获得谷歌关键字的位置?

时间:2013-04-28 22:22:36

标签: google-search

我有一个小脚本来获取谷歌的关键字位置:

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 个答案:

答案 0 :(得分: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      
相关问题