批量下载500首音乐走起(python多线程)分分钟下完

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值