我希望在以下代码中以JSON格式而不是XML返回数据,其中headers
是API的关键。 requests.get(translateUrl,headers=headers)
提供XML。
headers = {"Authorization ": finalToken}
translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text={}&to={}".format(textToTranslate, toLangCode)
translationData = requests.get(translateUrl, headers = headers)
# parse xml return values
translation = ElementTree.fromstring(translationData.text.encode('utf-8'))
# display translation
print (translation.text)