Django怎么调用有道词典或金山词典api获取单词发音?

我用Django写了一个小网站,当用户点击发音按钮的时候需要获取这一个单词的发硬,怎么调用呢?

下面就是有道字典获取发音的请求头,你只需要将下面hello换成你要查询的单词即可。
GET /dictvoice?audio=hello&type=2 HTTP/1.1
Accept: */*
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Referer: http://dict.youdao.com/search?q=hello&keyfrom=fanyi.smartResult
Accept-Language: zh-CN
Accept-Encoding: gzip, deflate
Host: dict.youdao.com
Connection: Keep-Alive追问

这个要怎么用啊?

追答

‘’‘两种方式:

    前端调用,即前端请求http://dict.youdao.com/dictvoice?audio=要查询的单词&type=2。

    另一种是后台请求,传给前台,我给你写下后台抓取的代码,Django视图你就自己写下:

’‘’

#coding=utf-8

import urllib
import urllib2

headers = {"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"}

url = "http://dict.youdao.com/dictvoice"

word = "hello"#你要查的单词
cuntry = "2" #美式为2,英式为1
params = {"audio":word, "type":cuntry}
data = urllib.urlencode(params)

request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)

fs = open("temp.wav", 'wb')
fs.write(response.read())#response.read()即是返回的音频流,你可以直接发给前台不用保存
fs.close()

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-19
我写了一个类似的网页,你肯出多少分呢追问

都给你都可以啊

追答

文哥不出马,大家都以为咱是病猫啊!这是可以查询英美发音且自带下载功能网页!

相似回答