import requestsif __name__ =="__main__": post_url='https://fanyi.baidu.com/v2transapi?from=en&to=zh' translation=input(' Your English to be translated is :') headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36' } data={ 'from': 'en', 'to': 'zh', 'query':translation, 'simple_means_flag': '3', 'sign': '871501.634748', 'token': 'a96496fe5d184896b689d4f248a711cb', 'domain':'common' } params={ 'from': 'en', 'to': 'zh' } response=requests.post(url=post_url,data=data,headers=headers,params=params) dic_obj=response.json() print(dic_obj)
The content of the error report is :
{'errno': 997, 'errmsg': ' Unknown error ', 'query': 'dog', 'from': 'en', 'to': 'zh', 'error': 997}
Is there any reverse climbing ?
Baidu translation has corresponding free API
https://fanyi.baidu.com/home
You can use the following link to register your application
import jsonimport randomimport hashlibfrom urllib import parseimport http.clientclass BaiduTranslate: def __init__(self,fromLang,toLang): self.url = "/api/trans/vip/translate" self.appid="***" # Account number applied for self.secretKey = '***'# Account and password self.fromLang = fromLang self.toLang = toLang self.salt = random.randint(32768, 65536) def BdTrans(self,text): sign = self.appid + text + str(self.salt) + self.secretKey md = hashlib.md5() md.update(sign.encode(encoding='utf-8')) sign = md.hexdigest() myurl = self.url + \ '?appid=' + self.appid + \ '&q=' + parse.quote(text) + \ '&from=' + self.fromLang + \ '&to=' + self.toLang + \ '&salt=' + str(self.salt) + \ '&sign=' + sign try: httpClient = http.client.HTTPConnection('api.fanyi.baidu.com') httpClient.request('GET', myurl) response = httpClient.getresponse() html = response.read().decode('utf-8') html = json.loads(html) dst = html["trans_result"][0]["dst"] return True , dst except Exception as e: return False , eif __name__=='__main__': BaiduTranslate_test = BaiduTranslate('zh','en') Results = BaiduTranslate_test.BdTrans(" Hello , The world !")# Phrases to translate print(Results)
If it's helpful, please click the button in the upper right corner , thank you