python 爬取网易云音乐歌单

Python 爬取网易云音乐

来自哔哩哔哩学习视频 爬取网易云音乐视频总结,也算是为自己记个笔记吧

  1. 所需库
    requests 点击查看介绍
    lxml 点击查看介绍

  2. 确定url地址
    以谷歌浏览器

    进入所需歌单,随便点首歌,右键,检查

    找到network—XHR,如果没有内容,刷新一下,接下来可看到如图

在这里插入图片描述
可看到所需歌曲的url
https://m801.music.126.net/20191203211801/8a4fe99967c7a8b03ef13992bed3e408/jdyyaac/075b/560e/515a/01d7ceed01adc38a2402f0bce5efa4fa.m4a

用浏览器直接浏览,可播放歌曲

也就是可以用python来访问这个网址

#导入库(框架,模块)
import requests

#1.确定url(网址,统一资源定位)地址
url = "https://m801.music.126.net/20191203211801/8a4fe99967c7a8b03ef13992bed3e408/jdyyaac/075b/560e/515a/01d7ceed01adc38a2402f0bce5efa4fa.m4a"

#2.请求
music=requests.get(url).content

#4.保存
#后面的wb,是允许写入二进制的
with open('mysic.m4a','wb') as file:
    file.write(music)

https://www.jianshu.com/p/c00df845323c with as 用法

下载成功

如果要下整个歌单,这种方法肯定是不行的
这是一场爬虫与反爬虫的较量

看到headers
为post请求,肯定会被加密
如图网址为外链地址
在这里插入图片描述
不过有个外链转化工具帮我们解决一切

在这里插入图片描述

干起来

#导入库(框架,模块)
import requests
from lxml import etree


#1.确定url(网址,统一资源定位)地址,歌单地址,利用xpath得到每首歌的外链地址
url="https://music.163.com/playlist?id=10702884"
base_url='https://link.hhtjim.com/163/'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
}

#2.请求
result=requests.get(url,headers=headers).text

#3.删选数据
dom=etree.HTML(result)
ids = dom.xpath('//a[contains(@href,"/song?")]/@href')
#print(ids)
#https://link.hhtjim.com/163/347230.mp3
for songid in ids:
    #print(songid)
    count_id=songid.strip('/song?id=')
    #print(count_id)
    if ('$' in count_id) == False:
        music_url = base_url+'%s'%count_id+'.mp3'
        print(music_url)

        music=requests.get(music_url).content
        #4.保存
        with open('./music_wangyi/%s.mp3'%count_id,'wb') as file:
            file.write(music)

下载成功
Vip的也可以下载,不过很容易被发现,但我们只是为了学习而已

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
基于Python Scrapy实现的网易云音乐music163数据爬取爬虫系统 含全部源代码 基于Scrapy框架的网易云音乐爬虫,大致爬虫流程如下: - 以歌手页为索引页,抓取到全部歌手; - 从全部歌手页抓取到全部专辑; - 通过所有专辑抓取到所有歌曲; - 最后抓取歌曲的精彩评论。 数据保存到`Mongodb`数据库,保存歌曲的歌手,歌名,专辑,和热评的作者,赞数,以及作者头像url。 抓取评论者的头像url,是因为如果大家喜欢,可以将他做web端。 ### 运行: ``` $ scrapy crawl music ``` #!/usr/bin/python #-*-coding:utf-8-*- import time from pprint import pprint from scrapy.spider import BaseSpider from scrapy.selector import HtmlXPathSelector from scrapy.http import Request from woaidu_crawler.items import WoaiduCrawlerItem from woaidu_crawler.utils.select_result import list_first_item,strip_null,deduplication,clean_url class WoaiduSpider(BaseSpider): name = "woaidu" start_urls = ( 'http://www.woaidu.org/sitemap_1.html', ) def parse(self,response): response_selector = HtmlXPathSelector(response) next_link = list_first_item(response_selector.select(u'//div[@class="k2"]/div/a[text()="下一页"]/@href').extract()) if next_link: next_link = clean_url(response.url,next_link,response.encoding) yield Request(url=next_link, callback=self.parse) for detail_link in response_selector.select(u'//div[contains(@class,"sousuolist")]/a/@href').extract(): if detail_link: detail_link = clean_url(response.url,detail_link,response.encoding) yield Request(url=detail_link, callback=self.parse_detail) def parse_detail(self, response): woaidu_item = WoaiduCrawlerItem() response_selector = HtmlXPathSelector(response) woaidu_item['book_name'] = list_first_item(response_selector.select('//div[@class="zizida"][1]/text()').extract()) woaidu_item['author'] = [list_first_item(response_selector.select('//div[@class="xiaoxiao"][1]/text()').extract())[5:].strip(),] woaidu_item['book_description'] = list_first_item(response_selector.select('//div[@class="lili"][1]/text()').extract()).strip() woaidu_item['book_covor_image_url'] = list
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值