Python 批量下载单词音频程序

前一段时间背TOEFL单词时为了能批量下载单词的音频,用Python实现了个小脚本(真的好方便啊^^)

读取words.txt文件,文件内部格式需要是一个单词一行,可以有空行。多个单词也可以,不过那样子是一个音频文件包含多个单词了

然后生成audio文件夹,文件命名为 “序号_单词”的形式。

 1 #coding=utf-8
 2 #Reading words from words.txt which contains single word in every line and downloading audio of those words from http://dict.youdao.com/dictvoice
 3 
 4 import urllib
 5 import urllib2
 6 import os
 7 
 8 headers = {"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"}
 9 
10 url = "http://dict.youdao.com/dictvoice"
11 
12 f = open("words.txt", "r")
13 count = 1
14 os.mkdir("audio")
15 os.chdir('audio')
16 while True:
17     line = f.readline()
18     if line:
19         if line=='\n': #ignore empty line
20             continue
21         word = line  #the word you gonna find
22         country = "2" #1 means British English,2 means Ameican English
23         params = {"audio":word, "type":country}
24         data = urllib.urlencode(params)
25 
26         request = urllib2.Request(url, data, headers)
27         response = urllib2.urlopen(request)
28         
29         fs = open("%d_%s.mp3"%(count,word.strip()), 'wb') #using counting number and word as file's name
30         fs.write(response.read())  #response.read() means return audio 
31         fs.close()
32         count = count + 1
33     else:
34         break
35 
36 f.close() 

 

posted on 2016-06-21 09:26  hogarth  阅读(147)  评论(0)    收藏  举报

导航