|
| 1 | +# Google Search Results JAVA API |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This Java package enables to scrape and parse Google, Bing and Baidu search results using [SerpApi](https://serpapi.com). Feel free to fork this repository to add more backends. |
| 6 | + |
| 7 | +This project is an implementation of SerpApi in Java 7. |
| 8 | +This code depends on GSON for efficient JSON processing. |
| 9 | +The HTTP response are converted to JSON Object. |
| 10 | + |
| 11 | +An example is provided in the test. |
| 12 | +@see src/test/java/GoogleSearchImplementationTest.java |
| 13 | + |
| 14 | +[The full documentation is available here.](https://serpapi.com/search-api) |
| 15 | + |
| 16 | +## Requirements |
| 17 | + |
| 18 | +Runtime: |
| 19 | + - Java / JDK 8+ (https://www.java.com/en/download/) |
| 20 | + Older version of java do not support HTTPS protocol. |
| 21 | + The SSLv3 is buggy which leads to Java raising this exception: javax.net.ssl.SSLHandshakeException |
| 22 | + |
| 23 | +For development: |
| 24 | + - Gradle 6.7+ (https://gradle.org/install/) |
| 25 | + |
| 26 | +## Maven / Gradle support |
| 27 | + |
| 28 | +Edit your build.gradle file |
| 29 | +```java |
| 30 | +repositories { |
| 31 | + maven { url "https://jitpack.io" } |
| 32 | +} |
| 33 | + |
| 34 | +dependencies { |
| 35 | + implementation 'com.github.serpapi:serpapi:1.0.0' |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +To list all the version available. |
| 40 | +https://jitpack.io/api/builds/com.github.serpapi/serpapi |
| 41 | + |
| 42 | +Note: jitpack.io enables to download maven package directly from github release. |
| 43 | + |
| 44 | +## Quick start |
| 45 | + |
| 46 | +To get started with this project in Java. |
| 47 | +We provided a fully working example. |
| 48 | +```bash |
| 49 | +git clone https://github.com/serpapi/serpapi-java.git |
| 50 | +cd serpapi-java/demo |
| 51 | +make run api_key=<your private key> |
| 52 | +``` |
| 53 | +Note: You need an account with SerpApi to obtain this key from: https://serpapi.com/dashboard |
| 54 | + |
| 55 | +file: demo/src/main/java/demo/App.java |
| 56 | +```java |
| 57 | +public class App { |
| 58 | + public static void main(String[] args) throws SerpApiException { |
| 59 | + if(args.length != 1) { |
| 60 | + System.out.println("Usage: app <secret api key>"); |
| 61 | + System.exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + String location = "Austin,Texas"; |
| 65 | + System.out.println("find the first Coffee in " + location); |
| 66 | + |
| 67 | + // parameters |
| 68 | + Map<String, String> parameter = new HashMap<>(); |
| 69 | + parameter.put("q", "Coffee"); |
| 70 | + parameter.put("location", location); |
| 71 | + parameter.put("apikey", args[0]); |
| 72 | + |
| 73 | + // Create search |
| 74 | + SerpApi serpapi = new SerpApi(parameter); |
| 75 | + |
| 76 | + try { |
| 77 | + // Execute search |
| 78 | + JsonObject data = search.getJson(); |
| 79 | + |
| 80 | + // Decode response |
| 81 | + JsonArray results = (JsonArray) data.get("local_results"); |
| 82 | + JsonObject first_result = results.get(0).getAsJsonObject(); |
| 83 | + System.out.println("first coffee: " + first_result.get("title").getAsString() + " in " + location); |
| 84 | + } catch (SerpApiException e) { |
| 85 | + System.out.println("oops exception detected!"); |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +This example runs a search about "coffee" using your secret api key. |
| 93 | + |
| 94 | +The Serp API service (backend) |
| 95 | + - searches on Google using the query: q = "coffee" |
| 96 | + - parses the messy HTML responses |
| 97 | + - return a standardized JSON response |
| 98 | +The class GoogleSearch |
| 99 | + - Format the request to Serp API server |
| 100 | + - Execute GET http request |
| 101 | + - Parse JSON into Ruby Hash using JSON standard library provided by Ruby |
| 102 | +Et voila.. |
| 103 | + |
| 104 | +Alternatively, you can search: |
| 105 | + - Bing using BingSearch class |
| 106 | + - Baidu using BaiduSearch class |
| 107 | + |
| 108 | +See the playground to generate your code. |
| 109 | + https://serpapi.com/playground |
| 110 | + |
| 111 | +## Example |
| 112 | + * [How to set SERP API key](#how-to-set-serp-api-key) |
| 113 | + * [Search API capability](#search-api-capability) |
| 114 | + * [Example by specification](#example-by-specification) |
| 115 | + * [Location API](#location-api) |
| 116 | + * [Search Archive API](#search-archive-api) |
| 117 | + * [Account API](#account-api) |
| 118 | + |
| 119 | +## How to set SERP API key |
| 120 | +The Serp API key can be set globally using a singleton pattern. |
| 121 | +```java |
| 122 | +GoogleSearch.serp_api_key_default = "Your Private Key" |
| 123 | +search = GoogleSearch(parameter) |
| 124 | +``` |
| 125 | +Or the Serp API key can be provided for each query. |
| 126 | + |
| 127 | +```java |
| 128 | +search = GoogleSearch(parameter, "Your Private Key") |
| 129 | +``` |
| 130 | + |
| 131 | +## Example with all params and all outputs |
| 132 | + |
| 133 | +```java |
| 134 | +query_parameter = { |
| 135 | + "q": "query", |
| 136 | + "google_domain": "Google Domain", |
| 137 | + "location": "Location Requested", |
| 138 | + "device": device, |
| 139 | + "hl": "Google UI Language", |
| 140 | + "gl": "Google Country", |
| 141 | + "safe": "Safe Search Flag", |
| 142 | + "num": "Number of Results", |
| 143 | + "start": "Pagination Offset", |
| 144 | + "serp_api_key": "Your SERP API Key", |
| 145 | + "tbm": "nws|isch|shop", |
| 146 | + "tbs": "custom to be search criteria", |
| 147 | + "async": true|false, // allow async request - non-blocker |
| 148 | + "output": "json|html", // output format |
| 149 | +} |
| 150 | + |
| 151 | +query = GoogleSearch.new(query_parameter) |
| 152 | +query.parameter.put("location", "Austin,Texas") |
| 153 | + |
| 154 | +String html_results = query.getHtml() |
| 155 | +JsonObject json_results = query.getJson() |
| 156 | +``` |
| 157 | + |
| 158 | +### Example by specification |
| 159 | + |
| 160 | +We love true open source, continuous integration and Test Drive Development (TDD). |
| 161 | + We are using RSpec to test [our infrastructure around the clock](https://travis-ci.org/serpapi/google-search-results-ruby) to achieve the best QoS (Quality Of Service). |
| 162 | + |
| 163 | +The directory test/ includes specification/examples. |
| 164 | + |
| 165 | +To run the test: |
| 166 | +```gradle test``` |
| 167 | + |
| 168 | + |
| 169 | +### Location API |
| 170 | + |
| 171 | +```java |
| 172 | +GoogleSearch search = new GoogleSearch(new HashMap<String, String()); |
| 173 | +JsonArray locationList = search.getLocation("Austin", 3); |
| 174 | +System.out.println(locationList.toString()); |
| 175 | +``` |
| 176 | +it prints the first 3 location matching Austin (Texas, Texas, Rochester) |
| 177 | + |
| 178 | +### Search Archive API |
| 179 | + |
| 180 | +Let's run a search to get a search_id. |
| 181 | +```java |
| 182 | +Map<String, String> parameter = new HashMap<>(); |
| 183 | +parameter.put("q", "Coffee"); |
| 184 | +parameter.put("location", "Austin,Texas"); |
| 185 | + |
| 186 | +GoogleSearch search = new GoogleSearch(parameter); |
| 187 | +JsonObject result = search.getJson(); |
| 188 | +int search_id = result.get("search_metadata").getAsJsonObject().get("id").getAsInt(); |
| 189 | +``` |
| 190 | + |
| 191 | +Now let retrieve the previous search from the archive. |
| 192 | +```java |
| 193 | +JsonObject archived_result = search.getSearchArchive(search_id); |
| 194 | +System.out.println(archived_result.toString()); |
| 195 | +``` |
| 196 | +it prints the search from the archive. |
| 197 | + |
| 198 | +### Account API |
| 199 | +Get account API |
| 200 | +```java |
| 201 | +GoogleSearch.serp_api_key_default = "Your Private Key" |
| 202 | +GoogleSearch search = new GoogleSearch(); |
| 203 | +JsonObject info = search.getAccount(); |
| 204 | +System.out.println(info.toString()); |
| 205 | +``` |
| 206 | +it prints your account information. |
| 207 | + |
| 208 | +## Build project |
| 209 | + |
| 210 | +### How to build from the source ? |
| 211 | + |
| 212 | +You must clone this repository. |
| 213 | +```bash |
| 214 | +git clone https://github.com/serpapi/serpapi-java.git |
| 215 | +``` |
| 216 | +Build the jar file. |
| 217 | +```bash |
| 218 | +gradle build |
| 219 | +``` |
| 220 | +Copy the jar to your project lib/ directory. |
| 221 | +```bash |
| 222 | +cp build/libs/serpapi-java.jar path/to/yourproject/lib |
| 223 | +``` |
| 224 | + |
| 225 | +### How to test ? |
| 226 | + |
| 227 | +```bash |
| 228 | +make test |
| 229 | +``` |
| 230 | + |
| 231 | +### Conclusion |
| 232 | + |
| 233 | +This service supports Google Images, News, Shopping. |
| 234 | +To enable a type of search, the field tbm (to be matched) must be set to: |
| 235 | + |
| 236 | + * isch: Google Images API. |
| 237 | + * nws: Google News API. |
| 238 | + * shop: Google Shopping API. |
| 239 | + * any other Google service should work out of the box. |
| 240 | + * (no tbm parameter): regular Google Search. |
| 241 | + |
| 242 | +[The full documentation is available here.](https://serpapi.com/search-api) |
| 243 | + |
| 244 | +Issue |
| 245 | +--- |
| 246 | +### SSL handshake error. |
| 247 | + |
| 248 | +#### symptom |
| 249 | + |
| 250 | +javax.net.ssl.SSLHandshakeException |
| 251 | + |
| 252 | +#### cause |
| 253 | +SerpApi is using HTTPS / SSLv3. Older JVM version do not support this protocol because it's more recent. |
| 254 | + |
| 255 | +#### solution |
| 256 | +Upgrade java to 1.8_201+ (which is recommended by Oracle). |
| 257 | + |
| 258 | + * On OSX you can switch versino of Java. |
| 259 | +```sh |
| 260 | +export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_201` |
| 261 | +java -version |
| 262 | +``` |
| 263 | + |
| 264 | + * On Windows manually upgrade your JDK / JVM to the latest. |
| 265 | + |
| 266 | +Changelog |
| 267 | +--- |
| 268 | +- 1.0.0 - Fully revisit API naming convention, and generalize client usage to match serpapi.com latest development |
| 269 | + |
| 270 | +Source |
| 271 | +--- |
| 272 | + * http://www.baeldung.com/java-http-request |
| 273 | + * https://github.com/google/gson |
| 274 | + |
| 275 | +Author |
| 276 | +--- |
| 277 | +Victor Benarbia - [email protected] |
0 commit comments