谷歌翻译的一个可用网页接口 - V2EX
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 12144 次点击
    这是一个创建于 3185 天前的主题,其中的信息可能已经有所发展或是发生改变。

    谷歌翻译是比较好用的服务,可是大部分时间我们都上不去,在 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
    可以参考一下这个:
    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     Solana     5361 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 31ms UTC 07:59 PVG 15:59 LAX 23:59 JFK 02:59
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86