python3使用urllib示例取googletranslate(谷歌翻译)
2014-01-23 00:00
1126 查看
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # File Name : gt1.py # Purpose : # Creation Date : 1390366260 # Last Modified : Wed 22 Jan 2014 06:14:11 PM CST # Release By : Doom.zhou 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"))
相关文章推荐
- python3使用urllib示例取googletranslate(谷歌翻译)
- urllib库的简单使用 && 一个简单的Python爬虫示例
- [python] 使用urllib从页面上采集信息示例
- Python3学习urllib的使用方法示例
- Python中使用urllib2模块编写爬虫的简单上手示例
- 03—小白学Python爬虫之urllib的基本和进阶使用及Get、Post示例
- Python中使用urllib2模块编写爬虫的简单上手示例
- python爬虫之urllib3的使用示例
- python3网络爬虫一《使用urllib.request发送请求》
- 使用Python的Flask框架构建大型Web应用程序的结构示例
- Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
- Python 标准库 urllib2 的使用细节
- python使用nsq简单示例
- Python使用win32 COM实现Excel的写入与保存功能示例
- Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
- python urllib的使用介紹
- 【再回首Python之美】【url/json小酌】查天气(使用urllib2,json模块)
- 使用Python写spark 示例
- python使用threading.Thread和Queue通过urllib2.urlopen抓取数据
- Python 标准库 urllib2 的使用细节(http://zhuoqiang.me/a/python-urllib2-usage)