开发者

Searching Google's localized web search with Java

I am trying to use a Java App to search Google in different countries, i.e. google.co.uk, google.de, etc. I found that the Google Ajax API used with Java will only let you do we开发者_StackOverflowb search with the following

URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
+ "q=information%20retrieval&key=INSERT-YOUR-KEY&userip=USERS-IP-ADDRESS");

URLConnection connection = url.openConnection();

connection.addRequestProperty("Referer", /* Enter the URL of your site here */);

however, that just gives me google.com's results. I need separate results for each country. Is there anyway to do this with Java.

Google has local search, but that's based on Google Maps, and it uses longitude and latitude, and provides business search results. I need web pages specific to each country.

Any ideas..

I was trying to scrape google.co.uk's search result with the following:

http://www.google.co.uk/#hl=en&source=hp&q=information+retrieval&aq=f&aqi=g10&aql=&oq=&gs_rfai=CjqZ0vBeYTLfwMZz0ygTJ84WADgAAAKoEBU_Qz3dV&fp=44fc429e19c3a006

but the returned result is an empty page. Any one know a workaround?

thanks

Sam


According to the class reference you can set a 'hl' parameter in the URL query. So for UK results you would specify hl=en-gb, and your code would look like this:

URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
+ "hl=en-gb&q=information%20retrieval&key=INSERT-YOUR-KEY&userip=USERS-IP-ADDRESS");

URLConnection connection = url.openConnection();

connection.addRequestProperty("Referer", /* Enter the URL of your site here */);

I am assuming here that the langauge affects the results, simply because these two requests (en v fr) appear to result in different results:

  • https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Google&hl=fr
  • https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Google&hl=en

You can find a lst of language codes here: http://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜