python爬虫练手:通过有道翻译实现快速翻译

from urllib import request,parse
import json
def fy():
    url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    key=input('请输入需要翻译的文字:')
    fromData={
        'i':key,
        'from':'AUTO',
        'smartresult': 'dict',
        'client': 'fanyideskweb',
        'salt': '15542578286104',
        'sign': '30719ff9989a6bcfafe8b3bc1e59a418',
        'ts': '1554257828610',
        'bv': '81eac1ea5e2a9ffd3d855ccd14e90bf6',
        'doctype': 'json',
        'version': '2.1',
        'keyfrom': 'fanyi.web',
        'action': 'FY_BY_CLICKBUTTION',
        'typoResult': 'false'
    }
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0'
    }
    data=parse.urlencode(fromData).encode('utf-8')
    req=request.Request(url,data=data,headers=headers)
    response=request.urlopen(req).read().decode()
    html=json.loads(response)
    fy=html['translateResult'][0][0]['tgt']
    print(fy)
if __name__=='__main__':
    while True:
        fy()

 

你可能感兴趣的:(python爬虫,python爬虫)