打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python翻译软件实现代码(使用google api完成)

python翻译软件实现代码(使用google api完成)

作者: 字体:[增加 减小] 类型:转载
这篇文章主要介绍了python结合google api完成的翻译软件实现代码,大家参考使用

复制代码 代码如下:

# -*- coding: utf-8 -*-
import httplib
from urllib import urlencode
import re

def out(text):
    p = re.compile(r'","')
    m = p.split(text)
    print m[0][4:].decode('UTF-8').encode('GBK')

if __name__=='__main__':
    while True:
        word=raw_input('Input the word you want to search:')
        text=urlencode({'text':word})
        h=httplib.HTTP('translate.google.cn')
        h.putrequest('GET', '/translate_a/t?client=t&hl=zh-CN&sl=en&tl=zh-CN&ie=UTF-8&oe=UTF-8&'+text)
        h.endheaders()
        h.getreply()
        f = h.getfile()
        lines = f.readlines()
        out(lines[0])
        f.close()

 haskell版

 

复制代码 代码如下:

 module Main where

import Network.HTTP
import Text.Regex.Posix

main = do
    putStrLn "Input the word you want to search:"
    word <- getLine
    handle <- simpleHTTP (getRequest $ "http://translate.google.cn/translate_a/t?client=t&hl=zh-CN&sl=en&tl=zh-CN&ie=UTF-8&oe=UTF-8&" ++ (text word))
    content <- getResponseBody handle
    let match = (content =~ "\",\""::(String,String,String))
    putStrLn $ drop 4 $ first match
    main

text word = urlEncodeVars [("text",word)]

first::(String,String,String)->String
first (x,_,_) = x

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Google TTS(文字转语音)api
很有个性的围脖
编织手法大集结
Python 实现一个自动化翻译和替换的工具
一件漂亮衣服及一个俄国网站-
解决F
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服