Python:有道翻译

# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class RedirectHandler(urllib2.HTTPRedirectHandler):
        def http_error_301(self,req,fp,code,msg,headers):
                pass
        def http_error_302(self,req,fp,code,msg,headers):
                pass
#创建Cookie        
cj = cookielib.CookieJar()

login_url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=https://www.baidu.com/link"
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 SE 2.X MetaSr 1.0"),                     
                     ("Accept-Language","zh-CN,zh;q=0.8"),("Accept","application/json, text/javascript, */*; q=0.01"),("X-Requested-With","XMLHttpRequest"),
                
]

resp = opener.open("http://fanyi.youdao.com/")
while True:
        value=raw_input('input english: ').decode('gbk')
        login_data =urllib.urlencode({"i":value,"type":"AUTO",'doctype':'json','xmlVersion':'1.8','keyfrom':'fanyi.web',"ue":'UTF-8','action':'FY_BY_ENTER','typoResult':'true'})
        resp = opener.open(login_url,login_data.encode('utf-8'))
        xml= resp.read().decode('utf-8')
        targ=json.loads(xml)
        print targ['translateResult'][0][0]['tgt']





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值