爬虫学习(爬取音乐)

复制代码
import re
import requests

url= "http://www.yy8844.cn/ting/numes/sussoc.shtml"
response = requests.get(url)
response.encoding = "gbk"
# print(r.text)
#第一步,访问网页获取MusicID
p = re.compile(r"MusicId=(.*?);",re.S)
print(re.search(p,response.text).group())

运行得到如下,获取到MusicId

复制代码
import re
import requests
import execjs
url= "http://www.yy8844.cn/ting/numes/sussoc.shtml"
response = requests.get(url)
response.encoding = "gbk"
# print(r.text)
#第一步,访问网页获取MusicID
p = re.compile(r"MusicId=(.*?);",re.S)
music_id = re.search(p,response.text).group(1)
#第二步 nodejs生成mp3 url
with open('m.js',encoding='utf-8') as f:
    ctx = execjs.compile(f.read())
    t = ctx.call("info", music_id)
    print(t)
#第三步 访问mp3 url,下载MP3并保存

function info(MusicId){
        var surl = "http://96.ierge.cn/";
        nurl = parseInt(MusicId / 30000) + "/" + parseInt(MusicId / 2000) + "/" + MusicId + ".mp3";
        fin_url = surl + nurl
        return fin_url
}
复制代码
import re
import execjs
import requests
from bs4 import BeautifulSoup as bs

def download_mp3(url,name):
    response = requests.get("http://www.yy8844.cn/"+url)
    response.encoding = "gbk"
    # print(r.text)
    # 第一步,访问网页获取MusicID
    p = re.compile(r"MusicId=(.*?);", re.S)
    music_id = re.search(p, response.text).group(1)
    # 第二步 nodejs生成mp3 url
    with open('m.js', encoding='utf-8') as f:
        ctx = execjs.compile(f.read())
        url = ctx.call("info", music_id)

        # 第三步 访问mp3 url,下载MP3并保存
        mp3_content = requests.get(url)
        with open(name+".mp3", "wb") as w:
            w.write(mp3_content.content)  # 获取文件(文件是二进制格式)并写入文件里


def get_index():
    url = "http://www.yy8844.cn/"
    headers = {
        "User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0"
    }
    r = requests.get(url, headers=headers)
    r.encoding = "gbk"
    fin_result = re.findall(r'<a href="(.*?)" target=\'musiclisten\'>(.*?)</a>', r.text)
    for i in fin_result:
        download_mp3(i[0],i[1])
        print("正在下载{}".format(i[1]))
        print("下载完成")

if __name__ == '__main__':
    get_index()

# soup = bs(r.text,'html.parser')
# print(soup.find_all(Class='link2'))
相关推荐
tyqtyq225 小时前
HarmonyOS AI 应用开发实战:考研择校分析系统
人工智能·学习·考研·华为·生活·harmonyos
光影6276 小时前
MySQL基础入门
数据库·笔记·sql·学习·mysql·学习方法
海兰6 小时前
【实用程序】网页公开信息智能采集系统详细设计指南
人工智能·学习·自动化·采集信息
MartinYeung56 小时前
[论文学习]AgentDAM:自主Web Agent的隐私泄露评估
学习
光影6276 小时前
MySQL 进阶篇 —— 事务 / 外键 / 索引 / 高级查询
数据库·笔记·sql·学习·mysql
晴雨天️8 小时前
Git工具使用指南
笔记·git
醉城夜风~8 小时前
MyBatis 基础CRUD全套实战学习笔记
笔记·学习·mybatis
blues92578 小时前
2026商超采购竹笋怎么选:从包装信息、货架周转到售后协同做判断
人工智能·科技·学习
Flandern11118 小时前
从“能跑”到“可运营”:Agent Harness 工程化建设指南
学习·agent·claudecode·harness
hj2862518 小时前
Shell 脚本完整学习笔记
chrome·笔记·学习