1. 程式人生 > >【Python學習】python爬蟲有道翻譯的實現

【Python學習】python爬蟲有道翻譯的實現

一、有道翻譯

1.1  實驗環境

        Anaconda2-4.3.1(Python2.7)

1.2  所需模組

        ①request

        ②json

1.3  一些細節

        跟古根翻譯同樣的,那就是在中譯英的過程中,如果碰到中文的句號或者感嘆號等使段落結束的標點符號,那麼就會切分翻譯,最後以元組返回,我做了一個很蹩腳的處理,就是直接用split()進行符號分割,取回每一句話進行翻譯,最後再串聯起來,具體可以看我的另外一篇博文Python爬蟲谷歌翻譯除此之外也可以自己看看返回的結構然後自己寫個正則化取回翻譯的內容

1.4 程式碼

#-*- coding:utf-8 -*


import requests
import json
import re


def get_translate_data(word=None):
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
    payload = {'type': 'AUTO', 'i': word, 'doctype': 'json', 'xmlVersion': 1.8,
               'keyfrom': 'fanyi.web', 'ue': 'UTF-8', 'action': 'FY_BY_CLICKBUTTON',
               'typoResult': 'true'
               }  # 建立資料字典
    response = requests.post(url, data=payload)
    # print response.text #返回字串

    content = json.loads(response.text)  # 將字串轉換為json資料
    return content['translateResult'][0][0]['tgt']



if __name__ == '__main__':
    texts=get_translate_data('進行復雜性研究的必讀入門書籍,原本以為這本書會跟普利高津的那本')
    results=get_translate_data(texts)
    print results

參考:http://m.blog.csdn.net/article/details?id=52900177&from=singlemessage