爬取某音乐榜单歌曲

一、打开网页https://music.163.com/,进入榜单(热歌榜)

二、右键检查、刷新网页,选择元素(点击歌曲名)

三、相关代码

python 复制代码
import requests
#正则表达式模块内置模块
import re
import os

filename = 'music\\'
if not os.path.exists(filename):
    os.mkdir(filename)
#如果想要爬取其他榜单的内容,只需要更改请求URL中的ID
url = 'https://music.163.com/discover/toplist?id=3778678'
#请求头
headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
response = requests.get(url=url,headers=headers)
# print(response.text)
html_data = re.findall('<li><a href="/song\?id=(\d+)">(.*?)</a>',response.text)

for num_id,title in html_data:
    music_url = f'http://music.163.com/song/media/outer/url?id={num_id}.mp3'
    #对于音乐播放地址发送请求,获取二进制数据内容
    music_content = requests.get(url=url,headers=headers).content
    with open(filename + title +'.mp3',mode='wb') as f:
        f.write(music_content)
    print(num_id,title)

四、爬取结果

相关推荐
心情好的小球藻28 分钟前
Python应用进阶DAY9--类型注解Type Hinting
开发语言·python
都叫我大帅哥29 分钟前
LangChain加载HTML内容全攻略:从入门到精通
python·langchain
惜.己40 分钟前
使用python读取json数据,简单的处理成元组数组
开发语言·python·测试工具·json
都叫我大帅哥2 小时前
Python的Optional:让你的代码优雅处理“空值”危机
python
曾几何时`4 小时前
基于python和neo4j构建知识图谱医药问答系统
python·知识图谱·neo4j
写写闲篇儿6 小时前
Python+MongoDB高效开发组合
linux·python·mongodb
杭州杭州杭州7 小时前
Python笔记
开发语言·笔记·python
路人蛃9 小时前
通过国内扣子(Coze)搭建智能体并接入discord机器人
人工智能·python·ubuntu·ai·aigc·个人开发
qiqiqi(^_×)9 小时前
卡在“pycharm正在创建帮助程序目录”
ide·python·pycharm
Ching·10 小时前
esp32使用ESP-IDF在Linux下的升级步骤,和遇到的坑Traceback (most recent call last):,及解决
linux·python·esp32·esp_idf升级