google custom search api 申请注册 cx key

本文档详细介绍了如何建立和使用谷歌自定义搜索引擎(CustomSearchEngine),包括获取APIkey,设置搜索参数,以及进行搜索测试。通过指定的搜索参数如key、cx和q,可以在应用程序中集成API进行搜索。同时,列出了各种搜索参数选项,如c2coff、cr、cx等,以定制化搜索需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.建立Custom Search Engine

2.到API key网站 获取 api key

3.测试 api key 搜索

4.搜索参数列表


1.建立Custom Search Engine

得到search engine ID,必须先建立 Custom Search Engine。

我们连到Custom Search Engine control panel 建立Search Engine。

https://cse.google.com/cse/all

https://ithelp.ithome.com.tw/upload/images/20191002/20112182dNLjRH1S9x.jpg

取得Search Engine ID , 把值复制下来

https://ithelp.ithome.com.tw/upload/images/20191003/201121829KqU5grMDU.jpg

 

2.到API key网站 获取 api key

https://developers.google.com/custom-search/v1/overview

https://ithelp.ithome.com.tw/upload/images/20191002/201121829e8XOtVVWY.jpg

取得API key

https://ithelp.ithome.com.tw/upload/images/20191002/20112182N3h97G8NBS.jpg

https://ithelp.ithome.com.tw/upload/images/20191003/20112182QRTLnS2xQ3.jpg

3.测试 api key 搜索

有了search engine ID & API key,我们将可采用以下网址搜寻Google 上丰富资料。

网址至少要包含以下三个参数:

key: API key

cx: search engine ID

q: 搜寻的文字

接下来我们就可以在浏览器输入要查询的资料,之后就可以在App中放入查询的API

get 搜索 实例:

https://www.googleapis.com/customsearch/v1?key=INSERT_YOUR_API_KEY&cx=017576662512468239146:omuauf_lfve&q=lectures

其中 INSERT_YOUR_API_KEY 换成自己 api key

https://ithelp.ithome.com.tw/upload/images/20191003/20112182xa7YuJlBLM.jpg

4.搜索参数列表

原文链接:https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list

Parameters
c2coff

string

Enables or disables Simplified and Traditional Chinese Search.

The default value for this parameter is 0 (zero), meaning that the feature is enabled. Supported values are:

  • 1: Disabled

  • 0: Enabled (default)

cr

string

Restricts search results to documents originating in a particular country. You may use Boolean operators in the cr parameter's value.

Google Search determines the country of a document by analyzing:

  • the top-level domain (TLD) of the document's URL

  • the geographic location of the Web server's IP address

See the Country Parameter Values page for a list of valid values for this parameter.

cx

string

The Programmable Search Engine ID to use for this request.

dateRestrict

string

Restricts results to URLs based on date. Supported values include:

  • d[number]: requests results from the specified number of past days.

  • w[number]: requests results from the specified number of past weeks.

  • m[number]: requests results from the specified number of past months.

  • y[number]: requests results from the specified number of past years.

exactTerms

string

Identifies a phrase that all documents in the search results must contain.

excludeTerms

string

Identifies a word or phrase that should not appear in any documents in the search results.

fileType

string

Restricts results to files of a specified extension. A list of file types indexable by Google can be found in Search Console Help Center.

filter

string

Controls turning on or off the duplicate content filter.

  • See Automatic Filtering for more information about Google's search results filters. Note that host crowding filtering applies only to multi-site searches.

  • By default, Google applies filtering to all search results to improve the quality of those results.

Acceptable values are:

  • 0: Turns off duplicate content filter.

  • 1: Turns on duplicate content filter.

gl

string

Geolocation of end user.

  • The gl parameter value is a two-letter country code. The gl parameter boosts search results whose country of origin matches the parameter value. See the Country Codes page for a list of valid values.

  • Specifying a gl parameter value should lead to more relevant results. This is particularly true for international customers and, even more specifically, for customers in English- speaking countries other than the United States.

googlehost

string

Deprecated. Use the gl parameter for a similar effect.

The local Google domain (for example, google.com, google.de, or google.fr) to use to perform the search.

highRange

string

Specifies the ending value for a search range.

  • Use lowRange and highRange to append an inclusive search range of lowRange...highRange to the query.
hl

string

Sets the user interface language.

  • Explicitly setting this parameter improves the performance and the quality of your search results.

  • See the Interface Languages section of Internationalizing Queries and Results Presentation for more information, and (Supported Interface Languages)[https://developers.google.com/custom-search/docs/xml_results_appendices#interfaceLanguages] for a list of supported languages.

hq

string

Appends the specified query terms to the query, as if they were combined with a logical AND operator.

imgColorType

enum (ImgColorType)

Returns black and white, grayscale, transparent, or color images. Acceptable values are:

  • "color"

  • "gray"

  • "mono": black and white

  • "trans": transparent background

imgDominantColor

enum (ImgDominantColor)

Returns images of a specific dominant color. Acceptable values are:

  • "black"

  • "blue"

  • "brown"

  • "gray"

  • "green"

  • "orange"

  • "pink"

  • "purple"

  • "red"

  • "teal"

  • "white"

  • "yellow"

imgSize

enum (ImgSize)

Returns images of a specified size. Acceptable values are:

  • "huge"

  • "icon"

  • "large"

  • "medium"

  • "small"

  • "xlarge"

  • "xxlarge"

imgType

enum (ImgType)

Returns images of a type. Acceptable values are:

  • "clipart"

  • "face"

  • "lineart"

  • "stock"

  • "photo"

  • "animated"

lowRange

string

Specifies the starting value for a search range. Use lowRange and highRange to append an inclusive search range of lowRange...highRange to the query.

lr

string

Restricts the search to documents written in a particular language (e.g., lr=lang_ja).

Acceptable values are:

  • "lang_ar": Arabic

  • "lang_bg": Bulgarian

  • "lang_ca": Catalan

  • "lang_cs": Czech

  • "lang_da": Danish

  • "lang_de": German

  • "lang_el": Greek

  • "lang_en": English

  • "lang_es": Spanish

  • "lang_et": Estonian

  • "lang_fi": Finnish

  • "lang_fr": French

  • "lang_hr": Croatian

  • "lang_hu": Hungarian

  • "lang_id": Indonesian

  • "lang_is": Icelandic

  • "lang_it": Italian

  • "lang_iw": Hebrew

  • "lang_ja": Japanese

  • "lang_ko": Korean

  • "lang_lt": Lithuanian

  • "lang_lv": Latvian

  • "lang_nl": Dutch

  • "lang_no": Norwegian

  • "lang_pl": Polish

  • "lang_pt": Portuguese

  • "lang_ro": Romanian

  • "lang_ru": Russian

  • "lang_sk": Slovak

  • "lang_sl": Slovenian

  • "lang_sr": Serbian

  • "lang_sv": Swedish

  • "lang_tr": Turkish

  • "lang_zh-CN": Chinese (Simplified)

  • "lang_zh-TW": Chinese (Traditional)

num

integer

Number of search results to return.

  • Valid values are integers between 1 and 10, inclusive.
orTerms

string

Provides additional search terms to check for in a document, where each document in the search results must contain at least one of the additional search terms.

q

string

Query

relatedSite

string

Specifies that all search results should be pages that are related to the specified URL.

rights

string

Filters based on licensing. Supported values include: cc_publicdomaincc_attributecc_sharealikecc_noncommercialcc_nonderived and combinations of these. See typical combinations.

safe

enum (Safe)

Search safety level. Acceptable values are:

  • "active": Enables SafeSearch filtering.

  • "off": Disables SafeSearch filtering. (default)

searchType

enum (SearchType)

Specifies the search type: image. If unspecified, results are limited to webpages.

Acceptable values are:

  • "image": custom image search.
siteSearchFilter

enum (SiteSearchFilter)

Controls whether to include or exclude results from the site named in the siteSearch parameter.

Acceptable values are:

  • "e": exclude

  • "i": include

sort

string

The sort expression to apply to the results. The sort parameter specifies that the results be sorted according to the specified expression i.e. sort by date. Example: sort=date.

start

integer (uint32 format)

The index of the first result to return. The default number of results per page is 10, so &start=11 would start at the top of the second page of results. Note: The JSON API will never return more than 100 results, even if more than 100 documents match the query, so setting the sum of start + num to a number greater than 100 will produce an error. Also note that the maximum value for num is 10.

 

 

 

 

 

<think>好的,用户想知道如何在浏览器或应用程序中添加自定义搜索引擎。我需要先理解这个问题的核心。自定义搜索引擎通常指的是让用户能够通过浏览器的搜索栏或应用程序内的搜索功能,使用特定的搜索引擎进行查询。比如,Chrome允许用户添加不同的搜索引擎,这样在地址栏输入关键词后可以直接搜索。 首先,我应该考虑不同浏览器的设置方法。比如,Chrome、Firefox、Edge等都有各自的添加方式。可能还需要提到基于Chromium的浏览器可能步骤类似。然后,对于应用程序,可能需要通过API集成,比如使用Google Custom Search JSON API或者Algolia等第三方服务。 接下来,我需要回忆相关知识。例如,在Chrome中添加自定义搜索引擎通常需要知道搜索引擎的URL结构,将搜索词替换为%s。例如,Google的搜索URL是https://www.google.com/search?q=%s。用户需要进入浏览器设置,找到搜索引擎管理部分,添加新的搜索引擎,设置关键词等步骤。 对于应用程序,开发者可能需要在前端设置表单,将用户输入的查询拼接到目标搜索引擎的URL中,或者使用API密钥进行认证,处理返回的JSON数据。例如,使用Google Custom Search API需要注册获取API密钥,然后发送HTTP请求,解析结果并展示。 还要注意引用中的内容。比如引用[2]提到登录后重定向到之前请求的页面,这可能和应用程序中的权限控制有关,但和当前问题关联不大。引用[5]提到创建Dialogflow代理,这可能和聊天机器人的搜索功能有关,但用户的问题更偏向于自定义搜索引擎的添加,可能需要简单提及集成到聊天机器人中的可能性。 用户可能需要分步骤的指导,包括浏览器端和应用程序端的不同方法。需要确保语言清晰,步骤准确。同时,生成的相关问题要符合用户可能进一步询问的方向,比如浏览器的具体步骤、API的使用方法、自定义搜索引擎的最佳实践等。 最后,检查是否有遗漏,比如移动端浏览器的处理,或者常见问题如URL格式错误导致搜索失败。确保回答覆盖主要平台,并提供示例帮助用户理解。</think>### 如何添加自定义搜索引擎 #### 在浏览器中添加自定义搜索引擎 1. **Chrome浏览器** - 打开设置 → 管理搜索引擎 → 添加新搜索引擎 - 格式示例:搜索URL为 `https://example.com/search?q=%s`,其中 `%s` 表示用户输入的查询词 - 设置关键词(如 `ex`),之后在地址栏输入 `ex [空格] 查询词` 即可触发该引擎[^2] 2. **Firefox浏览器** - 访问目标搜索引擎的网站,右键点击其搜索框 → “添加关键词” - 自定义名称和关键词,保存后可通过关键词调用 3. **Edge浏览器** - 设置 → 隐私、搜索和服务 → 地址栏和搜索 → 管理搜索引擎 - 添加包含 `%s` 占位符的URL模板 #### 在应用程序中集成自定义搜索引擎 1. **基于API的集成**(以Google Custom Search为例) - 注册Google Cloud账号并启用Custom Search JSON API[^5] - 获取API密钥和搜索引擎ID - 发送请求示例: ```python import requests api_key = "YOUR_KEY" cx = "SEARCH_ENGINE_ID" response = requests.get(f"https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}&cx={cx}") ``` 2. **前端实现** ```html <form action="https://example.com/search" method="GET"> <input type="text" name="q" placeholder="Search..."> <button type="submit">Search</button> </form> ``` #### 最佳实践 - 验证URL格式:确保占位符 `%s` 正确嵌入目标搜索引擎的查询参数 - 权限控制:涉及API调用时需实现认证机制,如OAuth2.0[^2][^4] - 结果渲染:处理API返回的JSON数据并设计友好的展示界面[^3]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值