手机
当前位置:查字典教程网 >脚本专栏 >python >python3使用urllib示例取googletranslate(谷歌翻译)
python3使用urllib示例取googletranslate(谷歌翻译)
摘要:复制代码代码如下:#!/usr/bin/envpython3#-*-coding:utf-8-*-#FileName:gt1.py#Purp...

复制代码 代码如下:

#!/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(谷歌翻译)】相关文章:

python中使用urllib2伪造HTTP报头的2个方法

python中的sort方法使用详解

python使用urllib2模块获取gravatar头像实例

python使用xauth方式登录饭否网然后发消息

python读取csv文件示例(python操作csv)

python中mechanize库的简单使用示例

python求素数示例分享

python使用cookielib库示例分享

Python代理抓取并验证使用多线程实现

python监控网卡流量并使用graphite绘图的示例

精品推荐
分类导航