#!/usr/bin/python
# -*- coding: utf-8 -*-
#coding=utf-8
import os,sys
import json
import
import httplib
key = '这里写上你在有道翻译api网站上得到的的apikey'
keyfrom = '同样是在有道翻译api网站获得'
doctype = 'json'
words = 'book'
#demo = http://fanyi.youdao.com/openapi.do?keyfrom=woodcol&key=1522180019&type=data&doctype=json&version=1.1&q=book
serverhttp = 'http://fanyi.youdao.com/openapi.do'
httpdemo = 'http://fanyi.youdao.com/openapi.do?keyfrom='+ keyfrom +'&key=' + key + '&type=data&doctype=' + doctype + '&version=1.1&q='
senddemo = httpdemo + words
per = 'keyfrom='+ keyfrom +'&key=' + key + '&type=data&doctype=' + doctype + '&version=1.1&q='
def
return 'test'
response = urllib2.urlopen(senddemo)
gettxt = response.read()
jsondic = json.loads(gettxt)
使用了这个api有一个好处,就是如果你有大量的文本要翻译,可以一次全翻译,也可以将这个http请求嵌入自已的网站应用或者手机应用中.下边是输出的结果.
{"translation":["书"],"basic":{"us-phonetic":"b?k","phonetic":"b?k","uk-phonetic":"b?k","explains":["n. 书籍;卷;帐簿;名册;工作簿","vt. 预订;登记","n. (Book)人名;(中)卜(广东话·威妥玛);(朝)北;(英)布克;(瑞典)博克"]},"query":"book","errorCode":0,"web":[{"value":["书籍","预定","图书"],"key":"book"},{"value":["书籍设计","书籍装帧设计","房地产画册"],"key":"Book design"},{"value":["参考书","工具书","参考工具书"],"key":"reference book"}]}
将json字符串转为python字典后的结果,中文因为编码格式不同,所以不能正确显示,不过这个不影响输出到文本文档.
{u'errorCode': 0, u'query': u'book', u'translation': [u'\u4e66'], u'web': [{u'key': u'book', u'value': [u'\u4e66\u7c4d', u'\u9884\u5b9a', u'\u56fe\u4e66']}, {u'key': u'Book design', u'value': [u'\u4e66\u7c4d\u8bbe\u8ba1', u'\u4e66\u7c4d\u88c5\u5e27\u8bbe\u8ba1', u'\u623f\u5730\u4ea7\u753b\u518c']}, {u'key': u'reference book', u'value': [u'\u53c2\u8003\u4e66', u'\u5de5\u5177\u4e66', u'\u53c2\u8003\u5de5\u5177\u4e66']}], u'basic': {u'phonetic': u'b\u028ak', u'us-phonetic': u'b\u028ak', u'explains': [u'n. \u4e66\u7c4d\uff1b\u5377\uff1b\u5e10\u7c3f\uff1b\u540d\u518c\uff1b\u5de5\u4f5c\u7c3f', u'vt. \u9884\u8ba2\uff1b\u767b\u8bb0', u'n. (Book)\u4eba\u540d\uff1b(\u4e2d)\u535c(\u5e7f\u4e1c\u8bdd\xb7\u5a01\u59a5\u739b)\uff1b(\u671d)\u5317\uff1b(\u82f1)\u5e03\u514b\uff1b(\u745e\u5178)\u535a\u514b'], u'uk-phonetic': u'b\u028ak'}}
下边有一个文章是关于http请求的: