网站设计模板安全吗,wordpress运行环境要求,网站的建设公司哪家好,网上做医生哪个网站好文章目录 1.文章概要1.1 实现方法1.2 实现代码1.3 最终效果 2.具体讲解2.1 使用的Python库2.2 代码说明2.2.1 创建目录保存文件2.2.2 爬取网易云音乐热歌榜单歌曲 2.3 过程展示 3 总结 1.文章概要
学习Python爬虫知识#xff0c;实现简单的一个小案例#xff0c;网易云音乐热… 文章目录 1.文章概要1.1 实现方法1.2 实现代码1.3 最终效果 2.具体讲解2.1 使用的Python库2.2 代码说明2.2.1 创建目录保存文件2.2.2 爬取网易云音乐热歌榜单歌曲 2.3 过程展示 3 总结 1.文章概要
学习Python爬虫知识实现简单的一个小案例网易云音乐热歌榜歌曲
1.1 实现方法
本文使用Python中常用的requests库来实现的
1.2 实现代码
以下是本项目全部代码
# author by mofitte
# vx:mofitte
# date 2024年11月13日import requests,re,osfilename music\\
if not os.path.exists(filename):os.makedirs(filename)url https://music.163.com/discover/toplist?id3778678 # 网易云音乐热歌榜单页面
headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36}response requests.get(url, headersheaders)
# print(response.text)
html_data re.findall(lia href/song\?id(\d)(.*?)/a, response.text)
for song_id, song_name in html_data:music_url fhttp://music.163.com/song/media/outer/url?id{song_id}.mp3# 对于音乐播放地址发送请求 获取二进制数据内容music_content requests.get(urlmusic_url, headersheaders).contentwith open(filename song_name .mp3, modewb) as f:f.write(music_content)print(song_id, song_name)print(爬虫任务已完成)1.3 最终效果 2.具体讲解
2.1 使用的Python库
1.requests一个简单易用的 Python 库用于发送 HTTP 请求 2.os: Python 的一个内置库提供了许多操作文件和目录的功能 3.re用于处理正则表达式它提供了一系列功能强大的函数用于字符串的搜索、替换、匹配等操作;
2.2 代码说明
2.2.1 创建目录保存文件
# author by mofitte
# vx:mofitte
# date 2024年11月13日import requests,re,os
filename music\\
if not os.path.exists(filename):os.makedirs(filename)在PC上创建filename目录(这里我用的是Windows系统),用于保存后续爬取下来的音乐文件
2.2.2 爬取网易云音乐热歌榜单歌曲
url https://music.163.com/discover/toplist?id3778678 # 网易云音乐热歌榜单页面
headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36}response requests.get(url, headersheaders)
# print(response.text)
html_data re.findall(lia href/song\?id(\d)(.*?)/a, response.text)
for song_id, song_name in html_data:music_url fhttp://music.163.com/song/media/outer/url?id{song_id}.mp3# 对于音乐播放地址发送请求 获取二进制数据内容music_content requests.get(urlmusic_url, headersheaders).content爬取音乐核心代码块 url这里我选择的是热歌榜单你也可以直接替换为你想要爬取的榜单直接运行也是可以的; headers模拟浏览器行为访问上述url这个没啥可说的 response获取响应这里是get了url和headers html_data获取音乐数据通过正则表达式匹配音乐id和音乐名称 music_url下载音乐路径可以在浏览器打开试听音乐
2.3 过程展示 3 总结
本案例是采用了requests库来简单获取数据下载音乐过程还是相对简单的更复杂的内容可能需要其他技术来实现继续学习。
看到这里了我只希望能点个赞谢谢 文章转载自: http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.fdfsh.cn.gov.cn.fdfsh.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.cjmmt.cn.gov.cn.cjmmt.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.zfwjh.cn.gov.cn.zfwjh.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.ljxps.cn.gov.cn.ljxps.cn http://www.morning.xyhql.cn.gov.cn.xyhql.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.hyryq.cn.gov.cn.hyryq.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.yptwn.cn.gov.cn.yptwn.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.qnklx.cn.gov.cn.qnklx.cn http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.rqmqr.cn.gov.cn.rqmqr.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.rklgm.cn.gov.cn.rklgm.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.ppllj.cn.gov.cn.ppllj.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.pdxqk.cn.gov.cn.pdxqk.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.rhpy.cn.gov.cn.rhpy.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn