Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中对非值对进行URL编码_Python_Json_Urlencode - Fatal编程技术网

在Python中对非值对进行URL编码

在Python中对非值对进行URL编码,python,json,urlencode,Python,Json,Urlencode,我试图在Python中使用Google的AJAX(JSON)Web搜索API。我被卡住了,因为Python的urllib.urlencode()只接受值对编码,而不是字符串本身。在Google的API中,查询字符串是搜索词,它与变量无关 query = "string that needs to be encoded" params = urllib.urlencode(query) # THIS FAILS # http://code.google.com/apis/ajaxsearch/do

我试图在Python中使用Google的AJAX(JSON)Web搜索API。我被卡住了,因为Python的urllib.urlencode()只接受值对编码,而不是字符串本身。在Google的API中,查询字符串是搜索词,它与变量无关

query = "string that needs to be encoded"
params = urllib.urlencode(query) # THIS FAILS
# http://code.google.com/apis/ajaxsearch/documentation/reference.html
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&%s&%s" % (params, GOOGLE_API_KEY)

request = urllib2.Request(url)
request.add_header('Referer', GOOGLE_REFERER)

search_results = urllib2.urlopen(request)
raw_results = search_results.read()
json = simplejson.loads(raw_results)
estimatedResultCount = json['responseData']['cursor']['estimatedResultCount']

if estimatedResultCount != 0:
    print "Google: %s hits" % estimatedResultCount

我该如何对我的搜索词进行URL编码?

我想你是在寻找