V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zzcchh
V2EX  ›  Python

谷歌翻译的一个可用网页接口

  •  
  •   zzcchh · 2017-04-10 13:45:47 +08:00 · 11234 次点击
    这是一个创建于 2563 天前的主题,其中的信息可能已经有所发展或是发生改变。

    谷歌翻译是比较好用的服务,可是大部分时间我们都上不去,在 stackflow 看到一个地址可以使用,在浏览器上输入 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=zh&dt=t&q=Hello'返回了一个 f.txt 。 打开文件,翻译的结果就在一个很怪格式里面:[[["你好","Hello",null,null,1]],null,"en"]如果把 hello 替换成其他句子,返回的也是对应的结果。 sl=‘源语言’, tl=‘翻译语言’ 我的问题是,想利用这个‘接口’,用 python 做一个 txt 文件翻译小工具,但是以上的网页我用 urlopen 打不开,抛出 HTTPError: HTTP Error 403: Forbidden ,请问各位 v2er 利用什么模块获得返回的文本呢?如果来进行编写呢?

    第 1 条附言  ·  2017-04-11 11:15:14 +08:00
    import requests

    filesource = open('c:\\source.txt')

    for line in filesource:



    url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=zh&dt=t&q='+line
    r = requests.get(url)
    r= r.text


    aa =r. split(',')

    print aa[0][4:-1],
    print aa[1][:-1]
    10 条回复    2017-04-13 23:17:09 +08:00
    jalena
        1
    jalena  
       2017-04-10 14:02:42 +08:00
    应该是验证了 UA 的。。
    JackyBao
        2
    JackyBao  
       2017-04-10 14:02:54 +08:00
    国内可以直接打开
    翻译服务 https://translate.google.cn
    horsley
        3
    horsley  
       2017-04-10 14:04:49 +08:00
    google cloud platform 上面有正经翻译 api 可用,收费
    yantze
        4
    yantze  
       2017-04-10 15:40:25 +08:00
    可以参考一下这个:
    https://www.v2ex.com/t/301209
    Kokororin
        5
    Kokororin  
       2017-04-10 15:43:48 +08:00
    dsg001
        6
    dsg001  
       2017-04-10 17:33:59 +08:00   ❤️ 2
    >>> import requests
    >>> url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=zh&dt=t&q=Hello+world'
    >>> r = requests.get(url)
    >>> print(r.text)
    [[["你好,世界","Hello world",null,null,1]],null,"en"]
    >>>
    cowpea
        7
    cowpea  
       2017-04-11 09:48:22 +08:00
    有道,扇贝都有 API ,扇贝还有读音
    zzcchh
        8
    zzcchh  
    OP
       2017-04-11 10:21:49 +08:00
    @cowpea 其实我们就是为了从围城里出去。
    yunkchen
        9
    yunkchen  
       2017-04-11 11:31:54 +08:00   ❤️ 1
    # -*- coding: UTF-8 -*-

    """
    @author:yunkchen
    @file:translate.py
    @time:2017/4/10 0010 下午 1:52
    """
    import requests
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    en2zh_url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=zh&dt=t&q="
    zh2en_url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=zh&tl=en&dt=t&q="
    headers = { "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Encoding":"gzip, deflate, sdch, br",
    "Accept-Language":"zh-CN,zh;q=0.8",
    "User-Agent":"Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
    "Referer": "http://wap.baidu.com"
    }


    def translate(word):
    word = word.decode("utf-8")
    if word >= u'\u4e00' and word<=u'\u9fa5':
    response = requests.get(zh2en_url+word, timeout=60, headers=headers)
    ch = response.content.split("\"")[1]
    print(ch)
    else:
    response = requests.get(en2zh_url+word, timeout=60, headers=headers)
    ch = response.content.split("\"")[1]
    print(ch)

    while True:
    word = raw_input("Please input word:")
    translate(word)
    yucongo
        10
    yucongo  
       2017-04-13 23:17:09 +08:00   ❤️ 1
    requests 的 response 里其实有 json 数据!

    不够钱帖源码, 参看 https://github.com/yucongo/mgoogle_translategoogleapis_translate.py ( Python 3.4 Win7 )
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5414 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 07:06 · PVG 15:06 · LAX 00:06 · JFK 03:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.