Leave a comment (1) 作者:小项-怪物猪

#!/usr/bin/env python
#-*-coding:utf-8-*-

import re,sys,urllib,urllib2

reload(sys)
sys.setdefaultencoding("utf-8")

url = "http://fanyi.youdao.com/openapi.do?keyfrom=&key=&type=data&doctype=json&version=1.1&q="

def tarn(txt):
    x = urllib2.Request(url+urllib.quote(txt))
    x.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19')
    x.add_header('Host','fanyi.youdao.com')
    x.add_header('Cookie','JSESSIONID=abcVYauywUcB-7YG-M0Ct; OUTFOX_SEARCH_USER_ID=*********@IP; SESSION_FROM=Buysz-blog')
    c = urllib2.urlopen(x)
    #print c.read()
    e_t = re.sub('\"|:|\{|\[|\]|translation|','',c.read().split(',')[0])
    return e_t

print tran('我是中国人')

分类:临时文件 | 标签: python, 翻译
现在有 1 条评论啦 »
  1. 发现json的更简单

    #!/usr/bin/env python
    #-*-coding:utf-8-*-

    import urllib2,urllib,sys,json

    reload(sys)
    sys.setdefaultencoding("utf-8")

    url = "http://fanyi.youdao.com/openapi.do?keyfrom=****&key=****&type=data&doctype=json&version=1.1&q="

    def tarn(txt):
    x = urllib2.Request(url+urllib.quote(txt))
    x.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19')
    x.add_header('Host','fanyi.youdao.com')
    c = urllib2.urlopen(x)
    e_t = json.loads(c.read())['translation'][0]
    return e_t

    print tarn('强大的天朝,无耻的富力集团,垃圾的天力物业;被坑的天朝小市民')

打劫!!留下你的评论 »

表情 emoticons

:?::razz::sad::evil::!::smile::oops::grin::eek::shock::???::cool::lol::mad::twisted::roll::wink::idea::arrow::neutral::cry::mrgreen:
  • 关闭

[ 搜狗云输入法 ]