SerpHubAPI

从 SerpApi 迁移

SerpApi 参数与返回字段到 SerpHubAPI 的对照表,改几行代码即可切换。

SerpHubAPI 沿用你熟悉的参数名(q、gl、hl、num),大多数 SerpApi 集成只需改 URL、鉴权请求头和几个字段名就能迁过来。

快速切换

之前(SerpApi):

import requests

r = requests.get("https://serpapi.com/search", params={
    "engine": "google",
    "q": "coffee",
    "gl": "us",
    "hl": "en",
    "num": 10,
    "api_key": SERPAPI_KEY,
})
for item in r.json().get("organic_results", []):
    print(item["position"], item["title"], item["link"])

之后(SerpHubAPI):

import requests

r = requests.post("https://api.serphubapi.com/v1/search",
    headers={"X-API-Key": SERPHUBAPI_KEY},          # shapi_xxxxxxxx
    json={
        "engine": "google",
        "q": "coffee",
        "gl": "us",
        "hl": "en",
        "num": 10,
    })
for item in r.json()["results"].get("organic", []):
    print(item["position"], item["title"], item["link"])

参数对照

SerpApiSerpHubAPI说明
GET https://serpapi.com/searchPOST https://api.serphubapi.com/v1/search用 JSON 请求体代替查询字符串
api_key(查询参数)X-API-Key 请求头(或 Authorization: Bearer)Key 不出现在 URL 中
qq相同。SerpApi 的 Yandex 用 text,我们所有引擎统一用 q
engine=googleengine=google,type=web
engine=bing / engine=yandexengine=bing / engine=yandex网页结果
engine=google_news 或 tbm=nwsengine=google,type=news
engine=google_images 或 tbm=ischengine=google,type=images
engine=google_videos 或 tbm=videngine=google,type=videos
engine=google_shopping 或 tbm=shopengine=google,type=shopping
engine=google_mapsengine=google,type=maps结果在 results.places
engine=google_scholarengine=google,type=scholar
engine=google_autocompleteengine=google,type=autocomplete结果在 results.suggestions
glgl相同
hlhl相同
numnum1–100
start(偏移量)page(从 1 开始)page = start / num + 1
tbs=qdr:d / qdr:w / qdr:m / qdr:ytime_range=day / week / month / year其他 tbs 取值无对应
no_cache=truemode=live(默认)
默认(允许返回缓存)mode=cache_first半价;命中缓存免费
output=html—需要文本渲染时用 format=markdown
location、uule、google_domain、device—不支持,请用 gl / hl
GET /searches/{id}.jsonGET /v1/searches/{id}存档保留 30 天
GET /account.jsonGET /v1/account

返回字段对照

SerpApiSerpHubAPI
search_metadata.idmeta.id
search_metadata.statusmeta.status
search_parametersparams
organic_resultsresults.organic
news_resultsresults.news
images_resultsresults.images(original → image)
video_resultsresults.videos
shopping_resultsresults.shopping(source → merchant)
local_resultsresults.places(title → name)
related_questionsresults.people_also_ask
related_searches[].queryresults.related_searches[](纯字符串)
knowledge_graphresults.knowledge_graph
error(字符串)error.code + error.message

结构差异

SerpApiSerpHubAPI
请求方式GET + 查询字符串POST + JSON 请求体
鉴权查询参数 api_key请求头 X-API-Key 或 Authorization: Bearer
选择引擎每个引擎、每个垂类一个 engine 值engine + type;engine=auto 由我们选择并自动切换
返回结构各引擎不同所有引擎统一一套结构
单次费用响应中不体现每个响应都有 meta.credits_charged
计费方式月度订阅预付积分;充值余额永不过期;空结果免费
网页正文不在搜索调用内搜索时加 include_content,或用 /v1/fetch
输出格式JSON 或 HTMLJSON 或 Markdown(format=markdown)
错误error 字符串{"error": {"code", "message"}},错误码稳定

迁移清单

  1. 在 /dashboard/keys 创建 Key。
  2. 改为 POST + JSON + X-API-Key。
  3. 把 engine=google_* / tbm 换成 type。
  4. 把 start 换成 page。
  5. 重命名返回字段(organic_results → results.organic 等)。
  6. 按 error.code 处理错误——见错误码。
  7. 抽几个查询对比结果,确认无误后切换流量。