出自:http://blog.csdn.net/zhaoyl03/article/details/8830806
最近想動手做一個文檔自動下載器,需要模擬瀏覽器的行為。雖然感覺思路上沒有困難,但在技術細節上需要自己一步一步試探。在網上搜索相關內容的過程中,發現有人用Python調用Google翻譯。我自己也試着實現這個小玩意,從而熟練和學習一些技術,如正則表達式匹配,模擬瀏覽器等。將這個小結果記錄下來,以激勵自己。
用Python調用Google翻譯,就是模擬人將原文本(英語)粘貼在Google翻譯的左邊文本框,選擇翻譯設置從英文到簡體中文,然后點擊翻譯,最后復制右邊文本框中的翻譯結果,並保存的過程。我比文獻《用Python實現調用Google翻譯》 的高明之處在,在提取翻譯后的結果時,用正則表達式匹配很輕巧地抓取到了翻譯后的文本。另外,代碼完整。
我用的Pyhon版本2.66,源碼如下:
- # -*- coding: utf-8 -*-
- #Python -V: Python 2.6.6
- #filename:GoogleTranslation1.2.py
- __author__ = "Yinlong Zhao (zhaoyl[at]sjtu[dot]edu[dot]cn)"
- __date__ = "$Date: 2013/04/21 $"
- import re
- import urllib,urllib2
- #urllib:
- #urllib2: The urllib2 module defines functions and classes which help in opening
- #URLs (mostly HTTP) in a complex world — basic and digest authentication,
- #redirections, cookies and more.
- def translate(text):
- '''''模擬瀏覽器的行為,向Google Translate的主頁發送數據,然后抓取翻譯結果 '''
- #text 輸入要翻譯的英文句子
- text_1=text
- #'langpair':'en'|'zh-CN'從英語到簡體中文
- values={'hl':'zh-CN','ie':'UTF-8','text':text_1,'langpair':"'en'|'zh-CN'"}
- url='http://translate.google.cn/translate_t'
- data = urllib.urlencode(values)
- req = urllib2.Request(url,data)
- #模擬一個瀏覽器
- browser='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'
- req.add_header('User-Agent',browser)
- #向谷歌翻譯發送請求
- response = urllib2.urlopen(req)
- #讀取返回頁面
- html=response.read()
- #從返回頁面中過濾出翻譯后的文本
- #使用正則表達式匹配
- #翻譯后的文本是'TRANSLATED_TEXT='等號后面的內容
- #.*? non-greedy or minimal fashion
- #(?<=...)Matches if the current position in the string is preceded
- #by a match for ... that ends at the current position
- p=re.compile(r"(?<=TRANSLATED_TEXT=).*?;")
- m=p.search(html)
- text_2=m.group(0).strip(';')
- return text_2
- if __name__ == "__main__":
- #text_1 原文
- #text_1=open('c:\\text.txt','r').read()
- text_1='Hello, my name is Derek. Nice to meet you! '
- print('The input text: %s' % text_1)
- text_2=translate(text_1).strip("'")
- print('The output text: %s' % text_2)
- #保存結果
- filename='c:\\Translation.txt'
- fp=open(filename,'w')
- fp.write(text_2)
- fp.close()
- report='Master, I have done the work and saved the translation at '+filename+'.'
- print('Report: %s' % report)
運行結果:
- >>>
- The input text: Hello, my name is Derek. Nice to meet you!
- The output text: 你好,我的名字是德里克。很高興見到你!
- Report: Master, I have done the work and saved the translation at c:\Translation.txt.
- >>>
感想:
1. 個人覺得Python的各種包稍顯混亂,需要很好的規范,才能更好的發展。
2. 若有想法,則從人類已有的“技術棧”尋找,進而實現
3. 用模擬瀏覽器模擬人的上網、瀏覽、按鍵行為在數據獲取階段非常重要
4. 熟悉一門語言不是一蹴而就的,需要代碼量,不斷積累。等有了很多磚頭塊后,才能在建造大廈時游刃有余。