Python調用Google翻譯


出自:http://blog.csdn.net/zhaoyl03/article/details/8830806

最近想動手做一個文檔自動下載器,需要模擬瀏覽器的行為。雖然感覺思路上沒有困難,但在技術細節上需要自己一步一步試探。在網上搜索相關內容的過程中,發現有人用Python調用Google翻譯。我自己也試着實現這個小玩意,從而熟練和學習一些技術,如正則表達式匹配,模擬瀏覽器等。將這個小結果記錄下來,以激勵自己。

用Python調用Google翻譯,就是模擬人將原文本(英語)粘貼在Google翻譯的左邊文本框,選擇翻譯設置從英文到簡體中文,然后點擊翻譯,最后復制右邊文本框中的翻譯結果,並保存的過程。我比文獻《用Python實現調用Google翻譯》 的高明之處在,在提取翻譯后的結果時,用正則表達式匹配很輕巧地抓取到了翻譯后的文本。另外,代碼完整。

 

我用的Pyhon版本2.66,源碼如下:

 

[python]  view plain  copy
 
  1. # -*- coding: utf-8 -*-  
  2. #Python -V: Python 2.6.6  
  3. #filename:GoogleTranslation1.2.py  
  4.   
  5. __author__ = "Yinlong Zhao (zhaoyl[at]sjtu[dot]edu[dot]cn)"    
  6. __date__ = "$Date: 2013/04/21 $"   
  7.   
  8. import re  
  9. import urllib,urllib2  
  10.   
  11. #urllib:  
  12. #urllib2: The urllib2 module defines functions and classes which help in opening  
  13. #URLs (mostly HTTP) in a complex world — basic and digest authentication,  
  14. #redirections, cookies and more.  
  15.   
  16.   
  17.   
  18. def translate(text):  
  19.       
  20.     '''''模擬瀏覽器的行為,向Google Translate的主頁發送數據,然后抓取翻譯結果 '''  
  21.       
  22.     #text 輸入要翻譯的英文句子  
  23.     text_1=text  
  24.     #'langpair':'en'|'zh-CN'從英語到簡體中文  
  25.     values={'hl':'zh-CN','ie':'UTF-8','text':text_1,'langpair':"'en'|'zh-CN'"}  
  26.     url='http://translate.google.cn/translate_t'  
  27.     data = urllib.urlencode(values)  
  28.     req = urllib2.Request(url,data)  
  29.     #模擬一個瀏覽器  
  30.     browser='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'  
  31.     req.add_header('User-Agent',browser)  
  32.     #向谷歌翻譯發送請求  
  33.     response = urllib2.urlopen(req)  
  34.     #讀取返回頁面  
  35.     html=response.read()  
  36.     #從返回頁面中過濾出翻譯后的文本  
  37.     #使用正則表達式匹配  
  38.     #翻譯后的文本是'TRANSLATED_TEXT='等號后面的內容  
  39.     #.*? non-greedy or minimal fashion  
  40.     #(?<=...)Matches if the current position in the string is preceded  
  41.     #by a match for ... that ends at the current position  
  42.     p=re.compile(r"(?<=TRANSLATED_TEXT=).*?;")  
  43.     m=p.search(html)  
  44.     text_2=m.group(0).strip(';')  
  45.     return text_2  
  46.   
  47. if __name__ == "__main__":  
  48.     #text_1 原文  
  49.     #text_1=open('c:\\text.txt','r').read()  
  50.     text_1='Hello, my name is Derek. Nice to meet you! '  
  51.     print('The input text: %s' % text_1)  
  52.     text_2=translate(text_1).strip("'")  
  53.     print('The output text: %s' % text_2)  
  54.   
  55.     #保存結果  
  56.     filename='c:\\Translation.txt'  
  57.     fp=open(filename,'w')  
  58.     fp.write(text_2)  
  59.     fp.close()  
  60.         
  61.     report='Master, I have done the work and saved the translation at '+filename+'.'  
  62.     print('Report: %s' % report)  



運行結果:

 

[python]  view plain  copy
 
  1. >>>   
  2. The input text: Hello, my name is Derek. Nice to meet you!   
  3. The output text: 你好,我的名字是德里克。很高興見到你!  
  4. Report: Master, I have done the work and saved the translation at c:\Translation.txt.  
  5. >>>   

 

 

感想:
1. 個人覺得Python的各種包稍顯混亂,需要很好的規范,才能更好的發展。

2. 若有想法,則從人類已有的“技術棧”尋找,進而實現

3. 用模擬瀏覽器模擬人的上網、瀏覽、按鍵行為在數據獲取階段非常重要

4. 熟悉一門語言不是一蹴而就的,需要代碼量,不斷積累。等有了很多磚頭塊后,才能在建造大廈時游刃有余。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM