{"id":4279,"date":"2011-09-27T19:05:32","date_gmt":"2011-09-27T13:35:32","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4279"},"modified":"2011-09-28T10:43:46","modified_gmt":"2011-09-28T05:13:46","slug":"how-to-get-google-indexed-pages-count-andor-do-google-searches-through-ajaxprogmatically","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-get-google-indexed-pages-count-andor-do-google-searches-through-ajaxprogmatically\/","title":{"rendered":"How to get Google Indexed Pages Count, and\/or do Google searches through AJAX\/programmatically"},"content":{"rendered":"<p>Hi,<br \/>\nRecently i had to <strong>get the count of the Pages Google has Indexed<\/strong> for a perticular web Site progmatically.<br \/>\nIn the process i found that Google offers an AJAX Search API to perform Google Searches.<br \/>\n.<br \/>\nHence i <strong>Implemented a Class<\/strong> which does the same for me easily(Ajax Search in Google).<br \/>\nAnd this Solution <strong>Does Google Searches programmatically<\/strong> <strong>and Also<\/strong> <strong>Give us the Indexed Pages Count<\/strong>.<br \/>\n<strong>Note:<\/strong> Its Groovy Implementation, Similar can be done in JavaScript using Ajax though intent will remain the same.<br \/>\n<em>The class has been named GoogleAjaxSearch because it uses the Google AJAX Search API \ud83d\ude09<\/em><br \/>\n<br \/>\nUse Cases Are As Follows:<br \/>\n[java]<br \/>\n\/\/Case 1 Simple Search<br \/>\nString searchQuery = &quot;my search Query&quot;<br \/>\nGoogleAjaxSearch googleAjaxSearch = new GoogleAjaxSearch(searchQuery)<br \/>\nprintln googleAjaxSearch.results \/\/Type List&lt;Expando&gt;, Just print it to see available properties<br \/>\n[\/java]<br \/>\n<br \/>\n[java]<br \/>\n\/\/Case 2: Get Number Of Indexed Pages in Google<br \/>\nString searchQuery = &quot;site:intelligrape.com&quot;<br \/>\nGoogleAjaxSearch googleAjaxSearch = new GoogleAjaxSearch(searchQuery)<br \/>\nprintln googleAjaxSearch.estimatedResultCount<br \/>\n[\/java]<br \/>\n<br \/>\n[java]<br \/>\n\/\/Case 3: Redo a search with same SearchQuery or a different Search Query<br \/>\nString searchQuery = &quot;search string&quot;<br \/>\nGoogleAjaxSearch googleAjaxSearch = new GoogleAjaxSearch(searchQuery)<br \/>\ngoogleAjaxSearch.redo() \/\/Re-Search with same query<br \/>\ngoogleAjaxSearch.redo(&quot;New Query&quot;) \/\/ Re-Search With New Query<br \/>\n[\/java]<br \/>\n<br \/>\n[java]<br \/>\n\/\/Case 4: All Details Available<br \/>\nString searchQuery = &quot;site:intelligrape.com&quot;<br \/>\nGoogleAjaxSearch googleAjaxSearch = new GoogleAjaxSearch(searchQuery)<br \/>\nprintln googleAjaxSearch.estimatedResultCount \/\/Gives the result count, indexed pages count<br \/>\nprintln googleAjaxSearch.currentPageIndex    \/\/Search result server side pagination &#8211; current page<br \/>\nprintln googleAjaxSearch.results  \/\/List&lt;Expando&gt; containing results<br \/>\nprintln googleAjaxSearch.responseStatus \/\/HTTP Status Code<br \/>\nprintln googleAjaxSearch.responseDetails \/\/Details for response, if any<br \/>\nprintln googleAjaxSearch.searchQuery     \/\/Search Query<br \/>\nprintln googleAjaxSearch.moreResultsUrl \/\/ Url to hit for next Page of results<br \/>\nprintln googleAjaxSearch.jsonResult \/\/ Complete raw JSON Result from google<br \/>\nprintln googleAjaxSearch.pages  \/\/ Pagination Pages Details<br \/>\n[\/java]<\/p>\n<p>The Class Code Is As Follows:<\/p>\n<hr>\n<p>[java]<br \/>\npackage myPackage.googleSearch<\/p>\n<p>import grails.converters.JSON<\/p>\n<p>class GoogleAjaxSearch {<\/p>\n<p>    public String searchQuery<br \/>\n    public String responseDetails<br \/>\n    public String moreResultsUrl<br \/>\n    public String jsonResult<\/p>\n<p>    public Integer responseStatus<br \/>\n    public Integer currentPageIndex<br \/>\n    public Integer estimatedResultCount<\/p>\n<p>    public def pages<\/p>\n<p>    public List&lt;Expando&gt; results = []<\/p>\n<p>    private static final String ajaxSearchTarget = &quot;http:\/\/ajax.googleapis.com\/ajax\/services\/search\/web?v=1.0&amp;q=###SEARCHQUERY###&amp;filter=0&quot;<\/p>\n<p>    public GoogleAjaxSearch(String searchQuery) {<br \/>\n        this.searchQuery = searchQuery<br \/>\n        search()<br \/>\n    }<\/p>\n<p>    public GoogleAjaxSearch() {<br \/>\n        this(null)<br \/>\n    }<\/p>\n<p>    public void redo() {<br \/>\n        search()<br \/>\n    }<\/p>\n<p>    public void redo(String newSearchQuery) {<br \/>\n        this.searchQuery = newSearchQuery<br \/>\n        search()<br \/>\n    }<\/p>\n<p>    private void search() {<br \/>\n        if (searchQuery) {<br \/>\n            URL url = new URL(ajaxSearchTarget.replace(&#8216;###SEARCHQUERY###&#8217;, searchQuery))<br \/>\n            URLConnection connection = url.openConnection()<br \/>\n            connection.setDoInput(true)<br \/>\n            InputStream inStream = connection.getInputStream()<br \/>\n            BufferedReader searchResultContent = new BufferedReader(new InputStreamReader(inStream))<br \/>\n            jsonResult = searchResultContent.getText()<br \/>\n            parseJsonAndPopulateObject()<br \/>\n        }<br \/>\n    }<\/p>\n<p>    private void parseJsonAndPopulateObject() {<br \/>\n        def jsonArray = JSON.parse(jsonResult)<br \/>\n        this.responseStatus = Integer.parseInt(jsonArray.responseStatus as String, 10)<br \/>\n        this.responseDetails = jsonArray.responseDetails<br \/>\n        this.moreResultsUrl = jsonArray.responseData.cursor.moreResultsUrl<br \/>\n        this.currentPageIndex = Integer.parseInt(jsonArray.responseData.cursor.currentPageIndex as String, 10)<br \/>\n        this.pages = jsonArray.responseData.cursor.pages<br \/>\n        this.estimatedResultCount = Integer.parseInt(jsonArray.responseData.cursor.estimatedResultCount as String, 10)<br \/>\n        results = jsonArray.responseData.results.collect {<br \/>\n            new Expando(<br \/>\n                    content: it.content,<br \/>\n                    GsearchResultClass: it.GsearchResultClass,<br \/>\n                    titleNoFormatting: it.titleNoFormatting,<br \/>\n                    title: it.title,<br \/>\n                    cacheUrl: it.cacheUrl,<br \/>\n                    unescapedUrl: it.unescapedUrl,<br \/>\n                    url: it.url,<br \/>\n                    visibleUrl: it.visibleUrl<br \/>\n            )<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>Hope That Helps \ud83d\ude42<br \/>\nRegards<br \/>\nKushal Likhi<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, Recently i had to get the count of the Pages Google has Indexed for a perticular web Site progmatically. In the process i found that Google offers an AJAX Search API to perform Google Searches. . Hence i Implemented a Class which does the same for me easily(Ajax Search in Google). And this Solution [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4279"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4279"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4279\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}