#search-engine #google-search #search #google #google-maps #scrape #api-integration

serpapi

Integrate search data into your Ruby application. This library is the official wrapper for SerpApi. SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and more.

1 stable release

1.0.0 Dec 28, 2023

#280 in HTTP client

MIT license

52KB
85 lines

SerpApi Search in Rust

CI serpapi-rust

This Rust package enables to scrape and parse search results from Google, Bing, Baidu, Yandex, Yahoo, Ebay, Apple, Youtube, Naver, Home depot and more. It's powered by SerpApi which delivered a consistent JSON format accross search engines. SerpApi.com enables to do localized search, leverage advanced search engine features and a lot more... A completed documentation is available at SerpApi.

Installation

To install in your rust application, update Cargo.toml

serpapi="1.0.0"

Usage

Let's start by searching for Coffee on Google:

// search example for google
//
use serpapi::serpapi::Client;
use std::collections::HashMap;
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Read your private API Key from an environment variable.
    // Copy/paste from [https://serpapi.com/dashboard] to your shell:
    // ```bash
    // export API_key="paste_your_private_api_key"
    // ```
    let api_key = match env::var_os("API_KEY") {
        Some(v) => v.into_string().unwrap(),
        None => panic!("$API_KEY environment variable is not set!"),
    };

    println!("let's initiliaze the client to search on google");
    let mut default = HashMap::new();
    default.insert("api_key".to_string(), api_key);
    default.insert("engine".to_string(), "google".to_string());
    // initialize the search engine
    let client = Client::new(default);

    // let's search for coffee in Austin, TX
    let mut parameter = HashMap::new();
    parameter.insert("q".to_string(), "coffee".to_string());
    parameter.insert("engine".to_string(), "google".to_string());
    // copy search parameter for the html search
    let mut html_parameter = HashMap::new();
    html_parameter.clone_from(&parameter);

    // search returns a JSON as serde_json::Value which can be accessed like a HashMap.
    println!("waiting...");
    let results = client.search(parameter).await?;
    let organic_results = results["organic_results"].as_array().unwrap();
    println!("results received");
    println!("--- JSON ---");
    let status = &results["search_metadata"]["status"];
    if status != "Success" {
        println!("search failed with status: {}", status);
    } else {
        println!("search is successfull");

        println!(" - number of organic_results: {}", organic_results.len());
        println!(
            " - organic_results first result description: {}",
            results["organic_results"][0]
        );

        // search returns text
        println!("--- HTML search ---");
        let raw = client.html(html_parameter).await.expect("html content");
        println!(" - raw HTML size {} bytes\n", raw.len());
        println!(
            " - async search completed with {}\n",
            results["search_parameters"]["engine"]
        );
    }

    print!("ok");
    Ok(())
}

Google search documentation. More hands on examples are available below.

Documentations

For more information how to build a paramaters HashMap see serpapi.com documentation

Location API

let default = HashMap::<String, String>::new();
let client = Client::new(default);
let mut parameter = HashMap::<String, String>::new();
parameter.insert("q".to_string(), "Austin".to_string());
let data = client.location(parameter).await.expect("request");
let locations = data.as_array().unwrap();

It returns the first 3 locations matching Austin (Texas, Texas, Rochester)

Search Archive API

let mut default = HashMap::<String, String>::new();
default.insert("engine".to_string(), "google".to_string());
default.insert("api_key".to_string(), "your_secret_key".to_string());
let client = Client::new(default);

// initialize the search engine
let mut parameter = HashMap::<String, String>::new();
parameter.insert("q".to_string(), "coffee".to_string());
parameter.insert(
    "location".to_string(),
    "Austin, TX, Texas, United States".to_string(),
);
let initial_results = client.search(parameter).await.expect("request");
let mut id = initial_results["search_metadata"]["id"].to_string();
// remove extra quote " from string convertion
id = id.replace("\"", "");

println!("{}", initial_results["search_metadata"]);
assert_ne!(id, "");

// search in archive
let archived_results = client.search_archive(&id).await.expect("request");
let archive_id = archived_results["search_metadata"]["id"].as_str();
let search_id = initial_results["search_metadata"]["id"].as_str();
println!("{}", archived_results);
assert_eq!(archive_id, search_id);

Account API

let client = Client::new(HashMap::<String, String>::new());
let mut parameter = HashMap::<String, String>::new();
parameter.insert("api_key".to_string(), "your_secret_key".to_string());
let account = client.account(parameter).await.expect("request");

It returns your account information.

Technical features

  • Dynamic JSON decoding using Serde JSON
  • Asyncronous HTTP request handle method using tokio and reqwest
  • Async tests using Tokio

References

Examples in rust

To run an example:

cargo build --example google_search

file: (examples/google_search.rs)

The keyword google can be replaced by any supported search engine:

  • google
  • baidu
  • bing
  • duckduckgo
  • yahoo
  • yandex
  • ebay
  • youtube
  • walmart
  • home_depot
  • apple_app_store
  • naver

Search bing

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "bing".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search baidu

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "baidu".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search yahoo

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "yahoo".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("p".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search youtube

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "youtube".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("search_query".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let video_results = results["video_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of video_results: {}", video_results.len());
    println!(
        " - video_results first result description: {}",
        results["video_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search walmart

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "walmart".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("query".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search ebay

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "ebay".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("_nkw".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search naver

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "naver".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("query".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let ads_results = results["ads_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of ads_results: {}", ads_results.len());
    println!(
        " - ads_results first result description: {}",
        results["ads_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search home depot

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "home_depot".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "table".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let products = results["products"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of products: {}", products.len());
    println!(
        " - products first result description: {}",
        results["products"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search apple app store

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "apple_app_store".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("term".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search duckduckgo

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "duckduckgo".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
parameter.insert("engine".to_string(), "google".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google scholar

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_scholar".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google autocomplete

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_autocomplete".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let suggestions = results["suggestions"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of suggestions: {}", suggestions.len());
    println!(
        " - suggestions first result description: {}",
        results["suggestions"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google product

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_product".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
parameter.insert("product_id".to_string(), "4887235756540435899".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let product_results = results["product_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of product_results: {}", product_results.len());
    println!(
        " - product_results first result description: {}",
        results["product_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google reverse image

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_reverse_image".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("image_url".to_string(), "https://i.imgur.com/5bGzZi7.jpg".to_string());
parameter.insert("max_results".to_string(), "1".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let image_sizes = results["image_sizes"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of image_sizes: {}", image_sizes.len());
    println!(
        " - image_sizes first result description: {}",
        results["image_sizes"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google events

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_events".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let events_results = results["events_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of events_results: {}", events_results.len());
    println!(
        " - events_results first result description: {}",
        results["events_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google local services

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_local_services".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "electrician".to_string());
parameter.insert("data_cid".to_string(), "6745062158417646970".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let local_ads = results["local_ads"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of local_ads: {}", local_ads.len());
    println!(
        " - local_ads first result description: {}",
        results["local_ads"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google maps

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_maps".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "pizza".to_string());
parameter.insert("ll".to_string(), "@40.7455096,-74.0083012,15.1z".to_string());
parameter.insert("type".to_string(), "search".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let local_results = results["local_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of local_results: {}", local_results.len());
    println!(
        " - local_results first result description: {}",
        results["local_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google jobs

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_jobs".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let jobs_results = results["jobs_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of jobs_results: {}", jobs_results.len());
    println!(
        " - jobs_results first result description: {}",
        results["jobs_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google play

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_play".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("q".to_string(), "kite".to_string());
parameter.insert("store".to_string(), "apps".to_string());
parameter.insert("max_results".to_string(), "2".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of organic_results: {}", organic_results.len());
    println!(
        " - organic_results first result description: {}",
        results["organic_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

Search google images

let mut default = HashMap::new();
default.insert("api_key".to_string(), "your_secret_api_key".to_string());
default.insert("engine".to_string(), "google_images".to_string());
// initialize the search engine
let client = Client::new(default);

// let's search for coffee in Austin, TX
let mut parameter = HashMap::new();
parameter.insert("engine".to_string(), "google_images".to_string());
parameter.insert("tbm".to_string(), "isch".to_string());
parameter.insert("q".to_string(), "coffee".to_string());
// copy search parameter for the html search
let mut html_parameter = HashMap::new();
html_parameter.clone_from(&parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let images_results = results["images_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
let status = &results["search_metadata"]["status"];
if status != "Success" {
    println!("search failed with status: {}", status);
} else {
    println!("search is successfull");

    println!(" - number of images_results: {}", images_results.len());
    println!(
        " - images_results first result description: {}",
        results["images_results"][0]
    );

    // search returns text
    println!("--- HTML search ---");
    println!(" - raw HTML size {} bytes\n", raw.len());
    println!(
        " - async search completed with {}\n",
        results["search_parameters"]["engine"]
    );
}

print!("ok");
Ok(())

License

MIT License

Continuous integration

We love "true open source" and "continuous integration", and Test Drive Development (TDD). We are using RSpec to test [our infrastructure around the clock]) using Github Action to achieve the best QoS (Quality Of Service).

The directory spec/ includes specification which serves the dual purposes of examples and functional tests.

Set your secret API key in your shell before running a test.

export API_KEY="your_secret_key"
cargo test

Contributions are welcome. Feel to submit a pull request!

Dependencies

~7–19MB
~259K SLoC