用Python實現的中英文互譯的小指令碼,

來源:互聯網
上載者:User

用Python實現的中英文互譯的小指令碼,

用Python實現的中英文互譯的小指令碼


剛開始學Python,覺得無聊,就想找個小程式練練手,那麼問題來了,寫什麼呢?


想到本人英語撇,每次遇到單詞都到有道詞典網站上去翻譯或者開啟手機app翻譯,覺得這樣很麻煩,而且花費的時間又長,所以。。。


首先開啟有道翻譯的網站,然後審查元素,找到network,然後隨便提交一個翻譯資訊,然後就瞪大眼珠子看著network,突然,一個post一閃而過


嘿嘿,逮到了,接下來,找到那個post的提交方式的那一項,點看,看他的資料是按照什麼格式提交的,然後用Python仿照它的方式提交就好了


就下來直接上代碼


#coding: utf-8import urllib.requestimport urllib.parseimport jsondef get_data(words):    data = {}    data["type"] = "AUTO"    data["i"] = words    data["doctype"] = "json"    data["xmlVersion"] = "1.8"    data["keyfrom:fanyi"] = "web"    data["ue"] = "UTF-8"    data["action"] = "FY_BY_CLICKBUTTON"    data["typoResult"] = "true"    data = urllib.parse.urlencode(data).encode('utf-8')    return datadef url_open(url, data):    req = urllib.request.Request(url, data)    req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36")    response = urllib.request.urlopen(req)    html = response.read()    html = html.decode("utf-8")    return htmldef get_json_data(html):    result = json.loads(html)    result = result['translateResult']    result = result[0][0]['tgt']    return resultdef main():    words = input("please input words: ")    url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=dict.top"    data = get_data(words)    html = url_open(url, data)    result = get_json_data(html)    print("The result: %s" % result)if __name__ == "__main__":    while True:        main()

哈哈,以後翻譯就方便多了,從此以後,就是用全英文的系統都不怕了,四六級就那麼的過了

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.