import requests from lxml import etree import re from multiprocessing.dummy import Pool PATH = r'E:\Englishmusic\\' # url = 'https://www.9ku.com/music/t_new.htm' url = 'https://www.9ku.com/music/t_hits.htm' headers = { 'User-Agent': '写你自己的' } res = requests.get(url, headers=headers) # print(res.text) selectors = etree.HTML(res.content) mp3list = selectors.xpath('//a[@class="songName "]/@href') namelist = selectors.xpath('//a[@class="songName "]/text()') dorman_url = 'https://www.9ku.com/' newlist = [] def down_link(mp3link): mp3link = mp3link.replace('play', 'down') res1 = requests.get(mp3link, headers) # print(res1.text) mp3_down = re.search('href="(.*?)" style="display:none"', res1.text).group(1) return mp3_down for i in range(len(mp3list)): key = down_link(dorman_url + mp3list[i]) value = namelist[i].strip() # dict[key] = value newlist.append((key, value)) def down_mp3(yuanzu): content = requests.get(yuanzu[0]).content with open(PATH + yuanzu[1] + '.mp3', 'wb') as f: f.write(content) print(yuanzu[1] + '下载完毕') if __name__ == '__main__': p = Pool(10) p.map(down_mp3, newlist)