我创建了一个 Python 项目来测试 google map api。我在测试第一个 API 时遇到了问题:附近的搜索请求。
我试过这个官方文档提供的网址: https ://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AddYourOwnKeyHere 如果我把这个网址在我的浏览器上,我会在浏览器上获得完美的数据。然后我什么也没做,只创建了一个 python 模块,代码如下:
if __name__ == '__main__':
import socket
import ssl
sock = ssl.wrap_socket(socket.socket())
sock.connect(('maps.googleapis.com', 443))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('maps.googleapis.com', 443))
sslSocket = socket.ssl(s)
uri = 'GET /maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=MyKey HTTP/1.1\r\nHost:maps.googleapis.com\r\nContent-Type: application/json; charset=UTF-8\r\naccept-encoding:gzip,deflate,sdch\r\n\r\n'
sslSocket.write(uri)
data = sock.read()
print 'going to connect...'
print data
s.close()
不幸的是,我在控制台上得到的结果是这样的:</p>
going to connect...
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Wed, 02 Oct 2013 08:05:59 GMT
Expires: Wed, 02 Oct 2013 08:10:59 GMT
Cache-Control: public, max-age=300
Vary: Accept-Language
Server: mafe
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 443:quic
Transfer-Encoding: chunked
560b
{
"debug_info" : [],
"html_attributions" : [
"Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"],
"next_page_token" : "ClRHAAAA4lOaMlZcUjyndIqhBsHzyyU0nD5PHarmjgmnguj3morn7Em- ipqhSR33V__d0kjrL4PzAqY7y4TAK7Uj3XzWjhoUchLBcBs- TNFxWIvGst8SEArmj_T7_eDZWyr8UDGy5OEaFPFaK2yHDg7Kv2U3Obsy7opkQfEw",
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.8599827,
"lng" : 151.2021282
},
"viewport" : {
"northeast" : {
"lat" : -33.8552584,
"lng" : 151.2031401
},
这个结果是不完整的,我试了很多次,结果都是一样的。那么,如何解决这个问题呢?