import urllib.request
import sys
typ = sys.getfilesystemencoding()
def translate(querystr, to_l="zh", from_l="en"):
'''for google tranlate by doom
'''
C_agent = {'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.165063 Safari/537.36 AppEngine-Google."}
flag = 'class="t0">'
tarurl = "http://translate.google.com/m?hl=%s&sl=%s&q=%s \
" % (to_l, from_l, querystr.replace(" ", "+"))
request = urllib.request.Request(tarurl, headers=C_agent)
page = str(urllib.request.urlopen(request).read().decode(typ))
target = page[page.find(flag) + len(flag):]
target = target.split("<")[0]
return target
print(translate("Hello world"))
</div>
您可能想查找下面的文章:
- 深入理解Python3中的http.client模块
- 【Python】Python的urllib模块、urllib2模块批量进行网页下载文件
- Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)
- Python网络编程中urllib2模块的用法总结
- Python使用urllib2模块抓取HTML页面资源的实例分享
- Python使用urllib2模块抓取HTML页面资源的实例分享
- 使用Python的urllib2模块处理url和图片的技巧两则
- 使用Python的urllib2模块处理url和图片的技巧两则
- Python中的urllib模块使用详解
- python使用urllib2提交http post请求的方法

