下面就是有道字典获取发音的请求头,你只需要将下面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.smartResultAccept-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()