(PHP) How to parse URLs in google search results?

…衆ロ難τιáo~ 提交于 2019-12-10 10:52:06

问题


How get google search results url?

(I use Zend_Gdata_Gbase for get search google results and not DomDocument/htmlsimpleparser because its looks to me that Zend_Gdata_Gbase done specially for parsing google results. if I wrong in my selection, please write.)

My function to get google search results for 'yahoo' or other query search string: (the function get a feed that should have search result for word 'yahoo', but when i use prin_t($feed) I don't see url for each result)

<?php    
function queryGoogleSearch($queryString='yahoo'){
            $service = new Zend_Gdata_Gbase();
            $query = $service->newSnippetQuery();
            $query->setBq('['.$queryString.']');
            $query->setOrderBy('modification_time');
            $query->setSortOrder('descending');
            $query->setMaxResults('4');
            $feed = $service->getGbaseSnippetFeed($query);
            return $feed; 
    }
    print_r(queryGoogleSearch());
?>

I get 4 first url results (when I search manually in google):

www.yahoo.com, mail.yahoo.com, search.yahoo.com, maps.yahoo.com

But I can't find them when I print $feed variable.

Please what should i change or add inqueryGoogleSearch() function? (Or other better code)

Thanks


回答1:


Are you trying to search google.com. Looks like that class is for Google Base, not google.com search engine. http://base.google.com/support/bin/answer.py?hl=en&answer=59260

You probably want this: http://code.google.com/apis/customsearch/v1/overview.html They recently just changed this. The old google search API has now deprecated as of Nov 1st. Custom search is the new API.

Its pretty simple to use without Zend.

http://code.google.com/apis/customsearch/v1/using_rest.html#WorkingResults

There is a JSON decoder in PHP. http://php.net/manual/en/function.json-decode.php

Hope that helps!




回答2:


The google search URL is pretty simple, I documented it here for a different reason. When you google something, for example 'blog' the page you are sent to is http://www.google.co.uk/search?q=blog. That bit after the ? is called the query string (in this case q=blog) which contains my search. Modifying this will modifiy googles search string, and return the appropriate results.

As for PHP you just need to add to the end of http://www.google.co.uk/search?q=



来源:https://stackoverflow.com/questions/4070247/php-how-to-parse-urls-in-google-search-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!