Python3--有道音频下载

2019-01-22 11:10:58 浏览数 (1)

上代码:

代码语言:javascript复制
#!/usr/bin/env python
from urllib.request import urlretrieve   
import time,random,requests  
from fake_useragent import UserAgent 
# import threading
# import sys


#获取IP列表并检验IP的有效性  
def get_ip_list():  
    f=open('IP.txt','r')  
    ip_list=f.readlines()  
    f.close()  
    return ip_list  
  
#从IP列表中获取随机IP  
def get_random_ip(ip_list):  
    proxy_ip = random.choice(ip_list)  
    proxy_ip=proxy_ip.strip('n')  
    proxies = {'https': proxy_ip}  
    return proxies  


def get_word_list():  
    f=open('names.txt','r')  
    word_list=f.readlines()  
    f.close()  
    return word_list 


def download(word,ip_list):
    url = "http://dict.youdao.com/dictvoice?audio=" word
    try:
        proxies = get_random_ip(ip_list)
        req = requests.get(url=url,proxies=proxies)
    except:
        proxies = get_random_ip(ip_list)
        req = requests.get(url=url,proxies=proxies)
    with open('音频库_2/{}.mp3'.format(word),'wb') as f:
        f.write(req.content)


def main ():
    ip_list = get_ip_list()
    word_list = get_word_list()
    for each in word_list:
        each = each.strip('n')
        download(each,ip_list)


if __name__ == '__main__':
	main()

0 人点赞