|
1 | | -<%- |
2 | | -def snippet(format, path, demo = false) |
3 | | - lines = File.new(path).readlines |
4 | | - if demo |
5 | | - # skip comment |
6 | | - buf = lines.reject { |line| line =~ /^\s*#|require 'serpapi'/ }[2..-1].join |
7 | | - else |
8 | | - # select test from line start to stop |
9 | | - start = 9 |
10 | | - stop = lines.find_index { |line| line =~ /pp results/} |
11 | | - slice = File.new(path).readlines[start..stop+1] |
12 | | - slice.reject! { |l| l =~ /expect\(|^\s*describe|^\s+it|^\s*end\s*$/ } |
13 | | - slice.map! { |l| l.gsub(/(^\s\s\s\s)/, '')} |
14 | | - buf = slice.join |
15 | | - buf.gsub!('# pp ', 'pp ') |
16 | | - buf.gsub!('api_key)', "ENV['SERPAPI_KEY'])") |
17 | | - end |
18 | | - %Q(```#{format}\nrequire 'serpapi'\n#{buf}```\n\n * source code: [#{path}](https://github.com/serpapi/serpapi-ruby/blob/master/#{path})) |
19 | | -end |
20 | | --%> |
21 | 1 | # SerpApi Ruby Library |
22 | 2 |
|
23 | 3 | [](https://github.com/serpapi/serpapi-ruby/actions/workflows/ci.yml) [](https://badge.fury.io/rb/serpapi) |
|
198 | 178 | # run user thread to search for your favorites coffee type |
199 | 179 | threads = %w(latte espresso cappuccino americano mocha macchiato frappuccino cold_brew).map do |query| |
200 | 180 | Thread.new do |
201 | | - pool.with { |socket| socket.search({q: query }).to_s } |
| 181 | + pool.with { |socket| socket.search(q: query).to_s } |
202 | 182 | end |
203 | 183 | end |
204 | 184 | responses = threads.map(&:value) |
@@ -339,156 +319,156 @@ It prints your account information as: |
339 | 319 | } |
340 | 320 | ``` |
341 | 321 |
|
342 | | -## Basic example per search engine |
| 322 | +## Examples |
| 323 | + |
| 324 | +Here are some examples for some of our most popular APIs. You can find the full list of supported engines and parameters in our [documentation](https://serpapi.com/search-engine-apis). |
| 325 | + |
| 326 | +### Google Shopping |
343 | 327 |
|
344 | | -### Search google |
| 328 | +Scrape Google Shopping results with product names, prices, ratings, and merchant information. |
345 | 329 |
|
346 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_spec.rb') %> |
347 | | -see: [https://serpapi.com/search-api](https://serpapi.com/search-api) |
| 330 | +```ruby |
| 331 | +require 'serpapi' |
348 | 332 |
|
349 | | -### Search google light |
350 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_light_spec.rb') %> |
351 | | -see: [https://serpapi.com/google-light-api](https://serpapi.com/google-light-api) |
| 333 | +client = SerpApi::Client.new(engine: 'google_shopping', api_key: ENV['SERPAPI_KEY']) |
| 334 | +results = client.search(q: 'Macbook M4') |
| 335 | +pp results[:shopping_results] |
| 336 | +``` |
352 | 337 |
|
353 | | -### Search google scholar |
354 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_scholar_spec.rb') %> |
355 | | -see: [https://serpapi.com/google-scholar-api](https://serpapi.com/google-scholar-api) |
| 338 | +[See documentation](https://serpapi.com/google-shopping-api) |
356 | 339 |
|
357 | | -### Search google autocomplete |
358 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_autocomplete_spec.rb') %> |
359 | | -see: [https://serpapi.com/google-autocomplete-api](https://serpapi.com/google-autocomplete-api) |
| 340 | +**Google Shopping Light** |
360 | 341 |
|
361 | | -### Search google product |
362 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_product_spec.rb') %> |
363 | | -see: [https://serpapi.com/google-product-api](https://serpapi.com/google-product-api) |
| 342 | +A [light variant](https://serpapi.com/google-shopping-light-api) engine called `google_shopping_light` is also available for faster, lower-cost shopping searches. |
364 | 343 |
|
365 | | -### Search google reverse image |
366 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_reverse_image_spec.rb') %> |
367 | | -see: [https://serpapi.com/google-reverse-image](https://serpapi.com/google-reverse-image) |
| 344 | +### Google Images |
368 | 345 |
|
369 | | -### Search google events |
370 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_events_spec.rb') %> |
371 | | -see: [https://serpapi.com/google-events-api](https://serpapi.com/google-events-api) |
| 346 | +Scrape Google Images search results, including image URLs, thumbnails, titles, and source pages. |
372 | 347 |
|
373 | | -### Search google local services |
374 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_local_services_spec.rb') %> |
375 | | -see: [https://serpapi.com/google-local-services-api](https://serpapi.com/google-local-services-api) |
| 348 | +```ruby |
| 349 | +require 'serpapi' |
376 | 350 |
|
377 | | -### Search google maps |
378 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_maps_spec.rb') %> |
379 | | -see: [https://serpapi.com/google-maps-api](https://serpapi.com/google-maps-api) |
| 351 | +client = SerpApi::Client.new(engine: 'google_images', api_key: ENV['SERPAPI_KEY']) |
| 352 | +results = client.search(q: 'coffee') |
| 353 | +pp results[:images_results] |
| 354 | +``` |
380 | 355 |
|
381 | | -### Search google jobs |
382 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_jobs_spec.rb') %> |
383 | | -see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api) |
| 356 | +[See documentation](https://serpapi.com/images-results) |
384 | 357 |
|
385 | | -### Search google play |
386 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_play_spec.rb') %> |
387 | | -see: [https://serpapi.com/google-play-api](https://serpapi.com/google-play-api) |
| 358 | +**Google Images Light** |
388 | 359 |
|
389 | | -### Search google images |
390 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_images_spec.rb') %> |
391 | | -see: [https://serpapi.com/images-results](https://serpapi.com/images-results) |
| 360 | +A [light variant](https://serpapi.com/google-images-light-api) engine called `google_images_light` is also available for faster, lower-cost image searches. |
392 | 361 |
|
393 | | -### Search google lens |
394 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_lens_spec.rb') %> |
395 | | -see: [https://serpapi.com/google-lens-api](https://serpapi.com/google-lens-api) |
| 362 | +### Google Trends |
396 | 363 |
|
397 | | -### Search google images light |
398 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_images_light_spec.rb') %> |
399 | | -see: [https://serpapi.com/google-images-light-api](https://serpapi.com/google-images-light-api) |
| 364 | +Track search interest over time and compare the popularity of search terms. |
400 | 365 |
|
401 | | -### Search google hotels |
402 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_hotels_spec.rb') %> |
403 | | -see: [https://serpapi.com/google-hotels-api](https://serpapi.com/google-hotels-api) |
| 366 | +```ruby |
| 367 | +require 'serpapi' |
404 | 368 |
|
405 | | -### Search google flights |
406 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_flights_spec.rb') %> |
407 | | -see: [https://serpapi.com/google-flights-api](https://serpapi.com/google-flights-api) |
| 369 | +client = SerpApi::Client.new(engine: 'google_trends', api_key: ENV['SERPAPI_KEY']) |
| 370 | +results = client.search(q: 'coffee', data_type: 'TIMESERIES') |
| 371 | +pp results[:interest_over_time] |
| 372 | +``` |
408 | 373 |
|
409 | | -### Search google finance |
410 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_finance_spec.rb') %> |
411 | | -see: [https://serpapi.com/google-finance-api](https://serpapi.com/google-finance-api) |
| 374 | +[See documentation](https://serpapi.com/google-trends-api) |
412 | 375 |
|
413 | | -### Search google ai overview |
414 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_ai_overview_spec.rb') %> |
415 | | -see: [https://serpapi.com/google-ai-overview-api](https://serpapi.com/google-ai-overview-api) |
| 376 | +### Google Flights |
416 | 377 |
|
417 | | -### Search google news |
418 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_news_spec.rb') %> |
419 | | -see: [https://serpapi.com/google-news-api](https://serpapi.com/google-news-api) |
| 378 | +Search flight routes, schedules, prices, and booking options. |
420 | 379 |
|
421 | | -### Search google news light |
422 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_news_light_spec.rb') %> |
423 | | -see: [https://serpapi.com/google-news-light-api](https://serpapi.com/google-news-light-api) |
| 380 | +> **Note:** The `google_flights` engine does not use `q`. Specify route and date parameters such as `departure_id`, `arrival_id`, `outbound_date`, and `return_date`. |
424 | 381 |
|
425 | | -### Search google patents |
426 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_patents_spec.rb') %> |
427 | | -see: [https://serpapi.com/google-patents-api](https://serpapi.com/google-patents-api) |
| 382 | +```ruby |
| 383 | +require 'date' |
| 384 | +require 'serpapi' |
428 | 385 |
|
429 | | -### Search google trends |
430 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_trends_spec.rb') %> |
431 | | -see: [https://serpapi.com/google-trends-api](https://serpapi.com/google-trends-api) |
| 386 | +outbound_date = (Date.today + 30).iso8601 |
| 387 | +return_date = (Date.today + 37).iso8601 |
| 388 | +client = SerpApi::Client.new(engine: 'google_flights', api_key: ENV['SERPAPI_KEY']) |
| 389 | +results = client.search( |
| 390 | + departure_id: 'LAX', |
| 391 | + arrival_id: 'AUS', |
| 392 | + outbound_date: outbound_date, |
| 393 | + return_date: return_date |
| 394 | +) |
| 395 | +flights = results[:best_flights] || results[:other_flights] |
| 396 | +pp flights |
| 397 | +``` |
432 | 398 |
|
433 | | -### Search google shopping |
434 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_shopping_spec.rb') %> |
435 | | -see: [https://serpapi.com/google-shopping-api](https://serpapi.com/google-shopping-api) |
| 399 | +[See documentation](https://serpapi.com/google-flights-api) |
436 | 400 |
|
437 | | -### Search google immersive product |
438 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_immersive_product_spec.rb') %> |
439 | | -see: [https://serpapi.com/google-immersive-product-api](https://serpapi.com/google-immersive-product-api) |
| 401 | +### Google AI Mode API |
440 | 402 |
|
441 | | -### Search google videos |
442 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_videos_spec.rb') %> |
443 | | -see: [https://serpapi.com/google-videos-api](https://serpapi.com/google-videos-api) |
| 403 | +The Google AI Mode API returns AI-generated answers with structured text blocks, references, images, products, and more. |
444 | 404 |
|
445 | | -### Search amazon |
446 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_amazon_spec.rb') %> |
447 | | -see: [https://serpapi.com/amazon-search-api](https://serpapi.com/amazon-search-api) |
| 405 | +```ruby |
| 406 | +require 'serpapi' |
448 | 407 |
|
449 | | -### Search baidu |
450 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_baidu_spec.rb') %> |
451 | | -see: [https://serpapi.com/baidu-search-api](https://serpapi.com/baidu-search-api) |
| 408 | +client = SerpApi::Client.new(engine: 'google_ai_mode', api_key: ENV['SERPAPI_KEY']) |
| 409 | +results = client.search(q: 'best coffee maker') |
| 410 | +pp results[:reconstructed_markdown] |
| 411 | +``` |
452 | 412 |
|
453 | | -### Search yahoo |
454 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yahoo_spec.rb') %> |
455 | | -see: [https://serpapi.com/yahoo-search-api](https://serpapi.com/yahoo-search-api) |
| 413 | +[See documentation](https://serpapi.com/google-ai-mode-api) |
456 | 414 |
|
457 | | -### Search youtube |
458 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_youtube_spec.rb') %> |
459 | | -see: [https://serpapi.com/youtube-search-api](https://serpapi.com/youtube-search-api) |
| 415 | +### Bing Search |
460 | 416 |
|
461 | | -### Search walmart |
462 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_walmart_spec.rb') %> |
463 | | -see: [https://serpapi.com/walmart-search-api](https://serpapi.com/walmart-search-api) |
| 417 | +Scrape Bing web search results, including organic results, ads, related searches, and more. |
464 | 418 |
|
465 | | -### Search ebay |
466 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_ebay_spec.rb') %> |
467 | | -see: [https://serpapi.com/ebay-search-api](https://serpapi.com/ebay-search-api) |
| 419 | +```ruby |
| 420 | +require 'serpapi' |
468 | 421 |
|
469 | | -### Search naver |
470 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_naver_spec.rb') %> |
471 | | -see: [https://serpapi.com/naver-search-api](https://serpapi.com/naver-search-api) |
| 422 | +client = SerpApi::Client.new(engine: 'bing', api_key: ENV['SERPAPI_KEY']) |
| 423 | +results = client.search(q: 'coffee') |
| 424 | +pp results[:organic_results] |
| 425 | +``` |
472 | 426 |
|
473 | | -### Search home depot |
474 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_home_depot_spec.rb') %> |
475 | | -see: [https://serpapi.com/home-depot-search-api](https://serpapi.com/home-depot-search-api) |
| 427 | +[See documentation](https://serpapi.com/bing-search-api) |
476 | 428 |
|
477 | | -### Search apple app store |
478 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_apple_app_store_spec.rb') %> |
479 | | -see: [https://serpapi.com/apple-app-store](https://serpapi.com/apple-app-store) |
| 429 | +### DuckDuckGo Search |
480 | 430 |
|
481 | | -### Search duckduckgo |
482 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_duckduckgo_spec.rb') %> |
483 | | -see: [https://serpapi.com/duckduckgo-search-api](https://serpapi.com/duckduckgo-search-api) |
| 431 | +Scrape DuckDuckGo search results, including organic results, ads, knowledge graphs, and related searches. |
484 | 432 |
|
485 | | -### Search yandex |
486 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yandex_spec.rb') %> |
487 | | -see: [https://serpapi.com/yandex-search-api](https://serpapi.com/yandex-search-api) |
| 433 | +```ruby |
| 434 | +require 'serpapi' |
| 435 | + |
| 436 | +client = SerpApi::Client.new(engine: 'duckduckgo', api_key: ENV['SERPAPI_KEY']) |
| 437 | +results = client.search(q: 'coffee') |
| 438 | +pp results[:organic_results] |
| 439 | +``` |
| 440 | + |
| 441 | +[See documentation](https://serpapi.com/duckduckgo-search-api) |
| 442 | + |
| 443 | +### Baidu Search |
| 444 | + |
| 445 | +Scrape Baidu search results, including organic results, answer boxes, and related searches. |
| 446 | + |
| 447 | +```ruby |
| 448 | +require 'serpapi' |
| 449 | + |
| 450 | +client = SerpApi::Client.new(engine: 'baidu', api_key: ENV['SERPAPI_KEY']) |
| 451 | +results = client.search(q: 'coffee') |
| 452 | +pp results[:organic_results] |
| 453 | +``` |
| 454 | + |
| 455 | +[See documentation](https://serpapi.com/baidu-search-api) |
| 456 | + |
| 457 | +### Amazon Search |
| 458 | + |
| 459 | +Scrape Amazon product search results, including product names, prices, ratings, reviews, and availability. |
| 460 | + |
| 461 | +> **Note:** The `amazon` engine uses the `k` parameter for a keyword search, not `q`. |
| 462 | + |
| 463 | +```ruby |
| 464 | +require 'serpapi' |
| 465 | + |
| 466 | +client = SerpApi::Client.new(engine: 'amazon', api_key: ENV['SERPAPI_KEY']) |
| 467 | +results = client.search(k: 'coffee') |
| 468 | +pp results[:organic_results] |
| 469 | +``` |
488 | 470 |
|
489 | | -### Search yelp |
490 | | -<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yelp_spec.rb') %> |
491 | | -see: [https://serpapi.com/yelp-search-api](https://serpapi.com/yelp-search-api) |
| 471 | +[See documentation](https://serpapi.com/amazon-search-api) |
492 | 472 |
|
493 | 473 | ## Performance Comparison |
494 | 474 |
|
@@ -539,7 +519,7 @@ client = SerpApi::Client.new(default_parameter) |
539 | 519 | search.params[:location] = "Portland,Oregon,United States" |
540 | 520 | # new way |
541 | 521 | # just provided the search call with the parameters. |
542 | | -results = client.search({location: "Portland,Oregon,United States", q: "Coffee"}) |
| 522 | +results = client.search(location: "Portland,Oregon,United States", q: "Coffee") |
543 | 523 |
|
544 | 524 | # search format return as raw html |
545 | 525 | # old way |
|
0 commit comments