學習python:執行個體1.百度翻譯

來源:互聯網
上載者:User

標籤:百度翻譯   python   request   urllib   

效果:

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/8D/06/wKioL1iDBIyAAxoaAAC4_e2cVNc748.gif" title="baidufanyi.gif" alt="wKioL1iDBIyAAxoaAAC4_e2cVNc748.gif" />


代碼:

# 百度翻譯# Python 3.5.1from urllib import request, parseimport jsonurl = ‘http://fanyi.baidu.com/v2transapi‘context = input(‘請輸入需要翻譯的內容 :\> ‘)if context >= ‘\u4e00‘ and context <= ‘\u9fa5‘:    # 判斷輸入內容是否為漢字    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(‘翻譯結果為:‘ + Result[‘trans_result‘][‘data‘][0][‘dst‘])


本文出自 “毛線的linux之路” 部落格,請務必保留此出處http://maoxian.blog.51cto.com/4227070/1893548

學習python:執行個體1.百度翻譯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.