Effect:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/8D/06/wKioL1iDBIyAAxoaAAC4_e2cVNc748.gif "title=" Baidufanyi.gif "alt=" Wkiol1idbiyaaxoaaac4_e2cvnc748.gif "/>
Code:
# Baidu Translation # python 3.5.1from urllib import request, parseimport jsonurl = ' Http://fanyi.baidu.com/v2transapi ' context = input (' Please enter the content to be translated :\> ') if context >= ' \u4e00 ' and context <= ' \u9fa5 ': # determine if the input content is Chinese characters From,To = ' zh ', ' en ' else: from,to = ' en ', ' zh ' data = { ' query ':context, ' from ' :from, ' to ':to, ' transtype ': ' Translang ', ' Simple_means_flag ': 3}data = parse.urlencode (data) encode (' Utf-8 ') r = request. Request (Url,data) r.add_header (' user-agent ', ' mozilla/5.0 (windows nt 10.0; win64; x64; rv:50.0) gecko/20100101 firefox/50.0 ') Html = request.urlopen (R). Read (). Decode (' Utf-8 ') result = json.loads(HTML) print (' translation result: ' + result[' trans_result ' [' Data '][0][' DST '])
This article is from the "Wool Linux Road" blog, please be sure to keep this source http://maoxian.blog.51cto.com/4227070/1893548
Learning Python: Example 1. Baidu Translation