调用百度api翻译英文网页

#!/usr/bin/python
import urllib.request
import urllib.parse
import urllib.response
import os
import sys
import bs4
import random
import wx

def getTransText(in_text):
    q = in_text
    fromLang = 'auto'  # 翻译源语言=自动检测
    toLang1 = 'auto'  # 译文语言 = 自动检测

    appid = '***************'
    salt = random.randint(32768, 65536)
    secretKey = '************'

    sign = appid + q + str(salt) + secretKey
    m1 = hashlib.md5(sign.encode('utf-8'))
    sign = m1.hexdigest()

    myurl = '/api/trans/vip/translate'
    myurl = myurl + '?appid=' + appid + '&q=' + q + '&from=' + fromLang + '&to=' + toLang1 + '&salt=' + str(
        salt) + '&sign=' + sign
    url = "http://api.fanyi.baidu.com" + myurl
    url = url.encode('utf-8')
    res = requests.get(url)

    res = eval(res.text)
    return (res["trans_result"][0]['dst'])


while (True):
    in_text = input()
    print(in_text + '  =  ' + getTransText(in_text))

response = urllib.request.urlopen("https://www.ericsson.com/en")
html = response.read()
soup = bs4.BeautifulSoup(html,"html.parser")

for title in soup.select('link'):
    print(title.get_text())

你可能感兴趣的:(学习)