爬虫——爬取小音乐网站

爬虫有几部分功能???

1.发请求,获得网页源码 #1.和2是在一步的 发请求成功了之后就能直接获得网页源码

2.解析我们想要的数据

3.按照需求保存

注意:开始爬虫前,需要给其封装

复制代码
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
}

爬虫分析:

第一步:从列表页抓取详情页面的链接

正则表达式:

复制代码
<li\sclass="media\sthread\stap\s\s".*?>.*?<div\sclass="subject\sbreak-all">.*?<a\shref="(.*?)">(.*?)</a>

得到如下结果

从以上结果可以看出,此链接不可直接点击,缺少https://www.hifini.com/这一部分

复制代码
https://www.hifini.com/thread-20945.htm

因此如下处理

for i in result:

print(i)#元祖下标取值

href = "https://www.hifini.com/"+i0

name = i1

print(href)

print(name)

print('======================')

第二步:获取歌曲播放资源

找到歌曲url的xpath:

复制代码
music:\s\[.*?title:\s'(.*?)',.*?url:\s'(.*?)'

代码:

复制代码
#解析歌曲的播放组员
song_re = "music:\s\[.*?title:\s'(.*?)',.*?url:\s'(.*?)'"
r = re.findall(song_re,song_html_data,re.S)
# print('歌曲信息',r)
for i in r:
    song_name = i[0]
    song_link = "https://www.hifini.com/"+i[1]
    print('歌名:',song_name)
    print("歌曲播放资源链接",song_link)
    print('++++++++++++++++')

第三步:再次像歌曲播放资源链接发请求 获得二进制数据,进行保存

1.创建文件夹

复制代码
#保存歌曲 先创建一个文件夹 导入os模块
#判断文件是否存在
if not os.path.exists('歌曲'):
    os.makedirs("歌曲")

2.创建文件流,将歌曲保存在文件夹中

复制代码
with open('歌曲\{}.m4a'.format(song_name),'wb')as f:
    f.write(data_bytes)

代码:

复制代码
import requests
import re
import os

shouye_url = 'https://www.hifini.com/'
# 1.起始目标
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
}
response = requests.get(shouye_url, headers=headers)
#1.发请求,获得网页源码
def get_data(url):
    response = requests.get(url,headers=headers)
    # print(response.status_code)
    # print(response.text)
    if response.status_code == 200:
        return response.text
#2.解析我们想要的数据
def parse_data(data):#形参站位 模拟的就是爬虫爬取下来的源码
    z ='<li\sclass="media\sthread\stap\s\s".*?>.*?<div\sclass="subject\sbreak-all">.*?<a\shref="(.*?)">(.*?)</a>'
    result = re.findall(z,data,re.S)
    # print(result)
    # https://www.hifini.com/thread-20945.htm
    for i in result:
        # print(i)#元祖下标取值
        href = "https://www.hifini.com/"+i[0]
        name = i[1]
        print(href)
        print(name)
        print('======================')
        get_song_link(href)
        #https://www.hifini.com/get_music.php?key=2Ydoqazb8E6jj+Nvl6rZLnuh3Fu1MRARle/srx5zQfZVMkPqsGrSzFHehon89oIENCUU19ru3GEJax60Ew
# 像详情页发请求 获得网页源码
def get_song_link(link):#link模拟的是详情页的url
    song_html_data = get_data(link)
    # print("详情页的网页源码",song_html_data)
    #解析歌曲的播放组员
    song_re = "music:\s\[.*?title:\s'(.*?)',.*?url:\s'(.*?)'"
    r = re.findall(song_re,song_html_data,re.S)
    # print('歌曲信息',r)
    for i in r:
        song_name = i[0]
        song_link = "https://www.hifini.com/"+i[1]
        print('歌名:',song_name)
        print("歌曲播放资源链接",song_link)
        print('++++++++++++++++')
        #再次像歌曲播放资源链接发请求 获得二进制数据
        data_bytes = requests.get(song_link,headers=headers).content
        # print(data_bytes)
        #保存歌曲 先创建一个文件夹 导入os模块
        #判断文件是否存在
        if not os.path.exists('歌曲'):
            os.makedirs("歌曲")
        with open('歌曲\{}.m4a'.format(song_name),'wb')as f:
            f.write(data_bytes)
#对应的功能写在不同的函数里面 如果需要互用功能 互相调用即可
if __name__ == '__main__':
    h = get_data(shouye_url)
    parse_data(h)

结果:

相关推荐
钱栈up17 小时前
长安汽车把海外年销钉在100万辆,2030年要翻到200万——出海从故事变成基建
爬虫·python·数据分析
李松桃2 天前
Python爬虫基础
开发语言·爬虫·python·get请求使用方法·post请求使用方法
步峰代理-盾山4 天前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
钱栈up4 天前
三星盯上散热老玩家Modine:AI算力链最后一块拼图要进自家口袋
爬虫·python·数据分析
tryCbest6 天前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python
狗都不学爬虫_6 天前
App逆向 - 希音 - 无感与图标验证码纯算
爬虫·python·网络爬虫
钱栈up6 天前
两台机器、一个 AI 记忆库:101 秒把 137 个文件对齐
爬虫·python·数据分析
钱栈up6 天前
外网通、QQ 登不上:一条 workbuddy 脚本塞进去的路由三天没还回来
爬虫·python·数据分析
choicesages7 天前
跑通不等于跑对:公开评论抓取里三处不报错的坐标问题
爬虫·python·数据清洗·数据质量·去重
钱栈up7 天前
烧结秤校准改造:一个 0.93 系数差点让整条日报线算错数
爬虫·python·数据分析