欢迎回来

登录 EAKE AI,继续您的智能之旅

忘记密码?
还没有账号?立即注册

SerpAPI 搜索引擎 — Google 搜索 API

SerpAPI 搜索引擎 — Google 搜索 API

通过 SerpAPI 获取 Google 搜索结果的结构化数据。支持网页、图片、新闻、视频搜索,让 Agent 具备实时搜索能力。

SerpApi for JavaScript/TypeScript

![npm version](https://www.npmjs.com/package/serpapi) ![Deno version](https://deno.land/x/serpapi) ![Build status](https://github.com/serpapi/serpapi-javascript/actions/workflows/build.yml) ![License](https://github.com/serpapi/serpapi-javascript/blob/master/LICENSE) ![SerpApi Libraries](https://serpapi.com/integrations)

Scrape and parse search engine results using SerpApi. Get search results from Google, Bing, Baidu, Yandex, Yahoo, Home Depot, eBay and more.

| 🪧 Coming from google-search-results-nodejs? <br /> Check out the migration document to find out how to upgrade. |

| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Quick start

Node.js

  • Supports Node.js 7.10.1 and newer.
  • Refer to
  • this example for help.

npm install serpapi
# or if you prefer yarn
yarn add serpapi
const { getJson } = require("serpapi");
getJson({
  engine: "google",
  api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
  q: "coffee",
  location: "Austin, Texas",
}, (json) => {
  console.log(json["organic_results"]);
});

Node.js with ES Modules (ESM) and top-level await

  • If you prefer using the import syntax and top-level await, you need to use
  • at least Node.js 14.8.0.

  • Refer to
  • this example for help.

You will need to add "type": "module" to your package.json:

{
  "type": "module",
  // rest of package.json
}
import { getJson } from "serpapi";
const response = await getJson({
  engine: "google",
  api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
  q: "coffee",
  location: "Austin, Texas",
});
console.log(response);

Deno

  • Import directly from deno.land.
  • Usage is otherwise the same as above.
  • Refer to
  • this example for help.

import { getJson } from "https://deno.land/x/serpapi/mod.ts";
const response = await getJson({
  engine: "google",
  api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
  q: "coffee",
  location: "Austin, Texas",
});
console.log(response);

Features

Configuration

You can declare a global api_key and timeout value by modifying the config object. timeout is defined in milliseconds and defaults to 60 seconds.

All functions, other than getLocations, accepts an optional api_key and timeout that will take precedence over the values defined in config.

getLocations doesn't require an API key.

import { config, getJson } from "serpapi";

config.api_key = API_KEY;
config.timeout = 60000;

await getJson({ engine: "google", q: "coffee" }); // uses the API key defined in the config
await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used

Using a Proxy

You can use a proxy by passing requestOptions with an HttpsProxyAgent instance. This can be done either globally through the config object or per-request in the parameters.

First, install the required package:

npm install https-proxy-agent
# or if you prefer yarn
yarn add https-proxy-agent

Then use it in your code:

import { config, getJson } from "serpapi";
import { HttpsProxyAgent } from "https-proxy-agent";

// Global configuration
config.requestOptions = {
  agent: new HttpsProxyAgent("http://proxy-server:port"),
};

// Or per-request configuration
await getJson({
  engine: "google",
  q: "coffee",
  requestOptions: {
    agent: new HttpsProxyAgent("http://proxy-server:port"),
  },
});

Pagination

Built-in pagination is not supported. Please refer to our pagination examples for a manual approach:

Functions

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

Table of Contents

  • [getJson](#getjson)
  • - [Parameters](#parameters) - [Examples](#examples)

  • [getHtml](#gethtml)
  • - [Parameters](#parameters-1) - [Examples](#examples-1)

  • [getJsonBySearchId](#getjsonbysearchid)
  • - [Parameters](#parameters-2) - [Examples](#examples-2)

  • [getHtmlBySearchId](#gethtmlbysearchid)
  • - [Parameters](#parameters-3) - [Examples](#examples-3)

  • [getAccount](#getaccount)
  • - [Parameters](#parameters-4) - [Examples](#examples-4)

  • [getLocations](#getlocations)
  • - [Parameters](#parameters-5) - [Examples](#examples-5)

getJson

Get a JSON response based on search parameters.

Parameters

  • parameters
  • object search query parameters for the engine

  • callback fn? optional callback

Examples

// single call (async/await)
const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });

// single call (callback)
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);

getHtml

Get a HTML response based on search parameters.

  • Accepts an optional callback.
  • Responds with a JSON string if the search request hasn't completed.

Parameters

  • parameters
  • object search query parameters for the engine

  • callback fn? optional callback

Examples

// async/await
const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });

// callback
getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);

getJsonBySearchId

Get a JSON response given a search ID.

  • This search ID can be obtained from the search_metadata.id key in the
  • response.

  • Typically used together with the async parameter.
  • Accepts an optional callback.

Parameters

  • searchId
  • string search ID

  • parameters
  • object (optional, default {})

- parameters.api_key string? API key - parameters.timeout number? timeout in milliseconds

  • callback fn? optional callback

Examples

const response = await getJson({
  engine: "google",
  api_key: API_KEY,
  asy

(内容节选自官方 README)

安装

# 安装到当前项目
npx skills add serpapi

# 全局安装
npx skills add serpapi -g

来源

安装指南

复制下方命令,在终端运行即可安装:

npx skills add serpapi
npx skills add serpapi -g
⚡ 一键安装到 GenHub

需已安装 GenHub 桌面端

使用指南

安装完成后,在对话框中直接使用此技能。

基本信息
作者 GenHub 分类 效率工具 难度 Beginner 时长 1 hour
🛠️ 安装命令
npx skills add serpapi
npx skills add serpapi -g

评论已关闭。