有道翻译爬虫

import urllib.request
import urllib.parse
import json

content = input('请输入要翻译的内容:')
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/'

head = {}
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'

data = {}
data['type'] = 'AUTO'
data['doctype'] = 'json'
data['i'] = content
data['keyfrom'] = 'fanyi.web'
data['typoresult'] = 'true'
data['ue'] = 'UTF-8'
data['xmlVersion'] = '1.8'

data = urllib.parse.urlencode(data).encode('utf-8')

req = urllib.request.Request(url,data,head)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
target = json.loads(html)

print(target['translateResult'][0][0]['tgt'])

 

posted @ 2016-05-15 13:27  疯陈演义  阅读(282)  评论(0编辑  收藏  举报