多线程爬取网易云音乐热歌榜 200首音乐

# 导入requests网络请求模块
import requests
# 导入lxml标签匹配模块
from lxml import etree
# 导入re 正则匹配模块
import re
#导入系统路径模块
import os
# 导入进程模块
import multiprocessing
import threading

class WangyiMusic(object):

    def data_content(self,url):
        data = requests.get(url,headers={'User-Agent': 'Mozilla/5.0'})
        return data.content

    def With(self):
        res = self.data_content('https://music.163.com/discover/toplist?id=3778678')
        res = res.decode('utf-8')
        with open('./index.html','w',encoding='utf-8') as f:
            f.write(res)

    def Xpath(self):
        with open('./index.html','r',encoding='utf-8') as f:
            html = f.read()
        res = re.findall('<li><a href="/song\?id=(.+?)">(.+?)</a></li>',html)
        my_list = []
        my_list_name = []
        for i in res:
            my_list.append("http://music.163.com/song/media/outer/url?id="+i[0]+".mp3")
            my_list_name.append(i[1])
        return my_list,my_list_name

    # def XiaZai(self,url,name):
    #     data = self.data_content(url)
    #
    def XiaZai(self, url, name):
        data = self.data_content(url)
        with open('E:/网易云top200/'+name+'.mp3','wb') as f:
            f.write(data)


if __name__ == '__main__':
    wang = WangyiMusic()
    a,b = wang.Xpath()
    for i in range(len(a)):
        t = threading.Thread(target=wang.XiaZai,args=(a[i],b[i]))
        t.start()
    t.join()

猜你喜欢

转载自www.cnblogs.com/Niuxingyu/p/10485460.html