python3使用urllib示例取googletranslate(谷歌翻译)

时间:2021-08-31 19:20:34 


#!/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
0
投稿

猜你喜欢

  • 初窥交互设计

    2009-10-10 10:52:00
  • 基于Python的EasyGUI学习实践

    2021-09-05 10:54:55
  • Django如何实现内容缓存示例详解

    2022-02-23 15:33:01
  • asp中判断服务器是否安装了某种组件的函数

    2011-02-16 10:53:00
  • Python实现公历(阳历)转农历(阴历)的方法示例

    2021-08-02 09:54:44
  • 详细解读Python的web.py框架下的application.py模块

    2021-06-24 22:28:47
  • 随机显示图片

    2009-07-26 10:13:00
  • 对Python3 goto 语句的使用方法详解

    2023-10-14 11:32:49
  • SQL SERVER如何判断某个字段包含大写字母

    2023-07-01 21:19:12
  • 互联网产品交互事件分析

    2009-10-06 15:23:00
  • php获取文件扩展名的3种方法实例

    2023-05-25 02:15:51
  • asp全面解析Server对象

    2008-10-19 17:24:00
  • tensorflow:指定gpu 限制使用量百分比,设置最小使用量的实现

    2021-06-03 17:02:32
  • python使用循环打印所有三位数水仙花数的实例

    2022-07-02 09:58:59
  • asp获取软件下载的真实地址!再谈获取Response.redirect重定向的URL!

    2010-03-10 22:03:00
  • 如何在Win下mysql备份恢复命令

    2010-03-03 17:23:00
  • 看看那些名牌LOGO的成长史

    2009-03-24 20:37:00
  • Python getattr()函数使用方法代码实例

    2022-03-04 03:30:51
  • 用户体验中的五大要素

    2008-07-07 16:41:00
  • python PIL模块的基本使用

    2022-08-28 16:35:19
  • asp之家 网络编程 m.aspxhome.com