一、谷歌
谷歌的搜索引擎需要自己创建服务才能启用检索api。(需自行翻墙和创建自己的谷歌账号)
1.1 API服务创建
1)登陆https://console.cloud.google.com/:

2) 选择新建项目,取号项目名即可(比如:OlineSearch)

3) 选择API和服务:

4)选择库

5) 搜索google search,并找到 custom search api

6)点击“启用”

7) 启用后点击“管理”或者直接从回退到主页面,点击“凭据”


8)创建凭据


9)将该凭据写入你的系统环境变量GOOGLE_SEARCH_API_KEY(留待后面程序中读取)

10) 登陆https://programmablesearchengine.google.com/创建可编辑程序引擎


11)将如下cx后字符串保存到你的系统环境变量CSE_ID(留待后面程序中读取)


1.2 服务使用样例(python)
import requests
import os
# Step 1.构建请求
url = "https://www.googleapis.com/customsearch/v1"
# Step 2.设置查询参数(还有很多参数)
search_term = "南京著名景点"
params = {
'q': search_term, # 搜索关键词
'key': os.getenv("GOOGLE_SEARCH_API_KEY"), # 谷歌搜索API Key
'cx': os.getenv("CSE_ID") # CSE ID
}
# Step 3.发送GET请求
response = requests.get(url, params=params)
# Step 4.解析响应
data = response.json()
完整参数可查阅官方文档
部分常用参数:

1.3 检索网址排除
除了通过参数排除外,推荐在可编辑程序引擎https://programmablesearchengine.google.com/中进行排除
点击你的服务,添加要排除的网站即可。

比如排除如下前缀网站:

1.4 返回结果样例
{
'kind': 'customsearch#search',
'url': {
'type': 'application/json',
'template': 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json'
},
'queries': {
'request': [{
'title': 'Google Custom Search - 南京著名景点',
'totalResults': '12500000',
'searchTerms': '南京著名景点',
'count': 10,
'startIndex': 1,
'inputEncoding': 'utf8',
'outputEncoding': 'utf8',
'safe': 'off',
'cx': 'xxxxxx'
}],
'nextPage': [{
'title': 'Google Custom Search - 南京著名景点',
'totalResults': '12500000',
'searchTerms': '南京著名景点',
'count': 10,
'startIndex': 11,
'inputEncoding': 'utf8',
'outputEncoding': 'utf8',
'safe': 'off',
'cx': 'xxxxxxxxxx'
}]
},
'context': {
'title': 'GoogleSearch'
},
'searchInformation': {
'searchTime': 0.316621,
'formattedSearchTime': '0.32',
'totalResults': '12500000',
'formattedTotalResults': '12,500,000'
},
'items': [{
'kind': 'customsearch#result',
'title': '南京市10 大景点玩乐- Tripadvisor',
'htmlTitle': '<b>南京</b>市10 大<b>景点</b>玩乐- Tripadvisor',
'link': 'https://cn.tripadvisor.com/Attractions-g294220-Activities-Nanjing_Jiangsu.html',
'displayLink': 'cn.tripadvisor.com',
'snippet': '南京市的热门景点. 了解推荐的选择方式 · 1. 侵华日军南京大屠杀遇难同胞纪念馆 · 1,083. 历史景点 · 2. 中山陵 · 1,700. 历史景点 · 3. 夫子庙景区 · 1,282. 地点与地标 · 4.',
'htmlSnippet': '<b>南京</b>市的热门<b>景点</b>. 了解推荐的选择方式 · 1. 侵华日军<b>南京</b>大屠杀遇难同胞纪念馆 · 1,083. 历史<b>景点</b> · 2. 中山陵 · 1,700. 历史<b>景点</b> · 3. 夫子庙景区 · 1,282. 地点与地标 · 4.',
'formattedUrl': 'https://cn.tripadvisor.com/Attractions-g294220-Activities-Nanjing_Jiangsu....',
'htmlFormattedUrl': 'https://cn.tripadvisor.com/Attractions-g294220-Activities-Nanjing_Jiangsu....',
'pagemap': {
'cse_thumbnail': [{
'src': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS4YWsFPvgOb5R2joL7RL5fNSVZTxO15lsA9xAQbsSA3dQGFSFBmD3pRQI&s',
'width': '259',
'height': '194'
}],
'metatags': [{
'twitter:app:url:iphone': 'tripadvisor://cn.tripadvisor.com/Attractions-g294220-Activities-Nanjing_Jiangsu.html?m=33762',
'apple-itunes-app': 'app-id=284876795, app-argument=https://cn.tripadvisor.com/Attractions-g294220-Activities-Nanjin

最低0.47元/天 解锁文章
252

被折叠的 条评论
为什么被折叠?



