Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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 谷歌方向API:不将地址翻译成英语_Python_Google Directions Api - Fatal编程技术网

Python 谷歌方向API:不将地址翻译成英语

Python 谷歌方向API:不将地址翻译成英语,python,google-directions-api,Python,Google Directions Api,我正在尝试使用Google Direction API服务。我的地址是中文的,不是英文的。如果我直接在浏览器中输入URL,Google会返回中文地址。但是,如果我在Python程序中包含URL,Google将把中文地址翻译成英文 在下文中,变量a、b、c和d是四个中文地址 from urllib.parse import quote from urllib.request import urlopen import json url = 'http://maps.googleapis.co

我正在尝试使用Google Direction API服务。我的地址是中文的,不是英文的。如果我直接在浏览器中输入URL,Google会返回中文地址。但是,如果我在Python程序中包含URL,Google将把中文地址翻译成英文

在下文中,变量
a
b
c
d
是四个中文地址

from urllib.parse   import quote
from urllib.request import urlopen
import json

url = 'http://maps.googleapis.com/maps/api/directions/json?'
a = '台中市霧峰區吉峰東路168號'
b = '桃園機場'
c = '台中市中區自由路一段1號'
d = '台中市大里區國光路一段1號'
url = (url +
      'origin=' + quote(a) +
      '&destination=' + quote(b) +
      '&waypoints=optimize:true|' + quote(c) + '|' + quote(d) + '&sensor=false')

print(url)
direction = urlopen(url).read().decode('utf-8')

方向
中的地址都翻译成英文地址。如何防止Google翻译地址?

您可以使用参数
语言

req_url = ("{}origin={}&destination={}&waypoints=optimize:true|{}|{}"
              "&sensor=false&language=zh-TW".format(
                  url, quote(a), quote(b), quote(c), quote(d)))

print(req_url)
direction = urlopen(req_url).read().decode('utf-8')


with open('result.txt', 'w') as ii:
    ii.write(direction.encode('utf-8'))