首页 编程教程正文

pathon小白实践第四天,爬音乐

piaodoo 编程教程 2020-02-22 22:07:40 954 0 python教程

本文来源吾爱破解论坛

第四天了,爬个音乐,嘿嘿,不多说,上源码,然后我要去学scrapy框架了,等我学好了,回来继续给大家分享。嗨学网嗨学网嗨学网
[Python] 纯文本查看 复制代码

import requests
import re
from lxml import etree
import os
class Spyder():
    def headers(self):
        headers={
                           'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
                }
        self.first_request(headers)
    def first_request(self,headers):
        url = 'http://music.taihe.com/artist'
        response = requests.get(url,headers=headers)
        html = etree.HTML(response.content.decode())
        art_name_list = html.xpath('//dt[@class="cover-img"]/a/img/@title')
        art_link_list = html.xpath('//dt[@class="cover-img"]/a/@href')
        for art_name,art_link in zip(art_name_list,art_link_list):
            if os.path.exists(art_name) == False: #如果当前没有Bigtit,就创建一个
                os.mkdir(art_name)

            self.second_request(art_name,art_link,headers)
    def second_request(self,art_name,art_link,headers):
        response = requests.get('http://music.taihe.com'+art_link,headers=headers)
        html1 = response.content.decode()
        html = etree.HTML(response.content.decode())
        song_name_list = html.xpath('//span[@class="songname"]/a/@title')
        song_link_list =re.compile('<a href="/song/(.*?)" class="songlist-songname namelink overdd  " ').findall(html1)

        for song_name,song_link in zip(song_name_list,song_link_list):
            self.load_music(song_name,song_link,art_name,headers)

    def load_music(self,song_name,song_link,art_name,headers):
        url= 'http://musicapi.taihe.com/v1/restserver/ting?method=baidu.ting.song.playAAC&format=jsonp&callback=jQuery17209000847668843108_1563975246089&songid='+song_link
        response = requests.get(url,headers=headers).content.decode()

        link =''.join(re.compile('{"show_link":"(.*?)",').findall(response)).replace('\\','')
        self.data_request(link,song_name,art_name,headers)
        # print("正在下载的歌曲是:".song_name)
    def data_request(self,link,song_name,art_name,headers):
        file_name =art_name +'\\' +song_name +'.mp3'
        print('正在下载的歌曲是:',song_name,'作者:',art_name)
        response = requests.get(link,headers=headers).content
        with open(file_name,'wb') as f:
            f.write(response)



spyder=Spyder()
spyder.headers()

QQ截图20190730131228.png (53.86 KB, 下载次数: 2)

下载附件  保存到相册

2019-7-30 13:12 上传

QQ截图20190730131228.png

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/7622.html

搜索