在python3中调用google 翻译api进行翻译(需要拥有google api密钥)

将英文翻译成中文,我在测试过程中发现google好像对传入的文本长度没有限制。

import  requests
import json

content = "Several years ago my health began to fail. I had to leave my job and, because I could no longer service my debts, I had to declare myself bankrupt in December 2015. I’m far from proud of this. A year later, I was discharged from bankruptcy and hoped to start to rebuild my creditworthiness.Most of my former creditors promptly updated my credit file and showed my accounts to be closed with no outstanding balance. However, Lloyds did not. It still erroneously reports I owe £9,311. For 18 months this has all but ruined any hope I have of rebuilding my credit profile. And it’s unlawful under both the Data Protection Act and, now, the GDPR. A couple of months ago, I learned I am terminally ill and I need to put my affairs in order. But despite receiving confirmation of my discharge from the Insolvency Service, Lloyds will not budge. CW, BedfordLloyds blamed a delay in receiving the discharge certificate from the Insolvency Service, but failed to explain why it took over a year to request this, while all other creditors managed to update your credit file immediately.It promised to perform the necessaries once the Observer intervened, but, weeks later, it had still not updated its own systems and, deeming you a debtor, refused to release the balance of a joint account held with your wife. It again insisted you present your discharge certificate.Back to Lloyds, which admitted the certificate had been requested in error, and allows the withdrawal along with a goodwill gesture to reflect its poor service."
 
language_type = "en"
url = "https://translation.googleapis.com/language/translate/v2"
    data = {
        'key': 'AIzaSyA************RBTNHg', #你自己的api密钥
        'source': language_type,
        'target': 'zh-cn',
        'q': content,
        'format': 'text'
    }
    headers = {'X-HTTP-Method-Override': 'GET'}
    response = requests.post(url, data=data, headers=headers)
    # print(response.json())
    res = response.json()
    # print(res["data"]["translations"][0]["translatedText"])
    text = res["data"]["translations"][0]["translatedText"]
    print(text)
    #return text

 

你可能感兴趣的:(第三方接口调用)