目前从百度翻译下载来的是 python2的

我把pyhon3 的贴出来.

# -*- coding:utf-8 -*-

from http.client import HTTPConnection
import hashlib
import urllib
import random

appid = '20151113000005349'
secretKey = 'osubCEzlGjzvw8qdQc41'


httpClient = None
myurl = '/api/trans/vip/translate'
q = 'apple'
fromLang = 'en'
toLang = 'zh'
salt = random.randint(32768, 65536)

sign = appid+q+str(salt)+secretKey
# m1 = md5.new()
m1 = hashlib.md5()
m1.update(sign.encode())
sign = m1.hexdigest()
myurl = myurl+'?appid='+appid+'&q='+urllib.parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign

try:
httpClient = HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)

#response是HTTPResponse对象
response = httpClient.getresponse()
print (response.read())
except Exception as e:
print (e)

finally:
if httpClient:
httpClient.close()

相关文章:

  • 2021-12-29
  • 2021-11-17
  • 2022-01-13
  • 2021-10-31
  • 2022-02-18
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-26
  • 2021-04-01
  • 2022-02-21
  • 2021-12-25
  • 2021-07-02
相关资源
相似解决方案