通过 API 的 Google 自定义搜索太慢

Vin*_*eph 5 query-performance google-custom-search

我正在使用 Google 自定义搜索为我的网站上的内容编制索引。

当我使用 REST 客户端发出 get 请求时

https://www.googleapis.com/customsearch/v1?key=xxx&q=query&cx=xx

我在亚秒内得到回复。

但是当我尝试使用我的代码拨打电话时,需要花费六秒钟的时间。我究竟做错了什么 ?

__author__ = 'xxxx'

import urllib2
import logging
import gzip

from cfc.apikey.googleapi import get_api_key
from cfc.url.processor import set_query_parameter
from StringIO import StringIO


CX = 'xxx:xxx'

URL = "https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=sd&fields=kind,items(title)" % (get_api_key(), CX)


def get_results(query):
    url = set_query_parameter(URL, 'q', query)
    request = urllib2.Request(url)
    request.add_header('Accept-encoding', 'gzip')
    request.add_header('User-Agent','cfc xxxx (gzip)')
    response = urllib2.urlopen(request)
    if response.info().get('Content-Encoding') == 'gzip':
        buf = StringIO(response.read())
        f = gzip.GzipFile(fileobj=buf)
        data = f.read()
        return data
Run Code Online (Sandbox Code Playgroud)

我已经实现了性能提示中提到的性能提示。我将不胜感激任何帮助。谢谢。