#搜索引擎 #谷歌搜索 #搜索 #谷歌 #抓取 #谷歌地图 #API绑定

serpapi

将搜索数据集成到您的Ruby应用程序中。此库是SerpApi的官方包装器。SerpApi支持谷歌、谷歌地图、谷歌购物、百度、Yandex、雅虎、易贝、应用商店等。

1个稳定版本

1.0.0 2023年12月28日

#188HTTP客户端

MIT 许可证

52KB
85

SerpApi在Rust中的搜索

CI serpapi-rust

此Rust软件包可从谷歌、必应、百度、Yandex、雅虎、易贝、苹果、YouTube、Naver、家得宝等网站抓取和解析搜索结果。它由SerpApi提供支持,SerpApi为所有搜索引擎提供了统一的JSON格式。SerpApi.com允许进行本地化搜索、利用高级搜索引擎功能等等...完整的文档可在SerpApi上找到。

安装

要在Rust应用程序中安装,请更新Cargo.toml

serpapi="1.0.0"

使用方法

让我们从在谷歌上搜索咖啡开始

// 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(())
}

谷歌搜索文档。下面有更多动手实践示例。

文档

有关如何构建参数HashMap的更多信息,请参阅serpapi.com文档

位置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();

它返回与奥斯汀(德克萨斯州,德克萨斯州,罗切斯特)匹配的前3个位置

搜索存档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);

账户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");

它返回您的账户信息。

技术特性

  • 使用Serde JSON进行动态JSON解码
  • 使用tokio和reqwest异步处理HTTP请求方法
  • 使用Tokio进行异步测试

参考资料

rust中的示例

运行示例

cargo build --example google_search

文件:(examples/google_search.rs)

关键字谷歌可以替换为任何受支持的搜索引擎

  • 谷歌
  • 百度
  • 必应
  • duckduckgo
  • 雅虎
  • yandex
  • ebay
  • youtube
  • walmart
  • home_depot
  • apple_app_store
  • naver

搜索必应

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(())

搜索百度

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(())

搜索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(())

搜索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(())

搜索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(())

搜索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(())

搜索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(())

搜索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(())

搜索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(())

搜索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(())

搜索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(())

搜索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(())

搜索Google自动完成

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(())

搜索Google产品

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(())

搜索Google反向图片

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(())

搜索Google事件

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(())

搜索Google本地服务

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(())

搜索Google地图

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(())

搜索Google工作

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(())

搜索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(())

搜索谷歌图片

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(())

许可证

MIT许可证

持续集成

我们热爱“真正的开源”和“持续集成”,以及测试驱动开发(TDD)。我们使用RSpec测试我们的基础设施(全天候),通过GitHub Action实现最佳QoS(服务质量)。

spec/目录包含规范,它既提供示例,也提供功能测试。

在运行测试之前,请在您的shell中设置您的秘密API密钥。

export API_KEY="your_secret_key"
cargo test

欢迎贡献。请随时提交pull请求!

依赖项

约5-20MB
约268K SLoC