爬虫学习(爬取音乐)

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'))

相关推荐

  1. Pytho音乐

    2024-03-30 07:12:03       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-30 07:12:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-30 07:12:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-30 07:12:03       82 阅读
  4. Python语言-面向对象

    2024-03-30 07:12:03       91 阅读

热门阅读

  1. 服务未注册到nacos通过gateway转发的配置

    2024-03-30 07:12:03       38 阅读
  2. MYSQL中update的low_priority

    2024-03-30 07:12:03       37 阅读
  3. 机器学习 - 提高模型 (代码)

    2024-03-30 07:12:03       37 阅读
  4. ARM.day8

    2024-03-30 07:12:03       38 阅读
  5. 怎么使用vuex的数据和方法

    2024-03-30 07:12:03       36 阅读
  6. 使用VHDL实现俄罗斯方块游戏设计

    2024-03-30 07:12:03       43 阅读
  7. PyTorch中的flatten+transpose函数说明

    2024-03-30 07:12:03       45 阅读
  8. 使用Dom4j解析多层级XML为Map对象

    2024-03-30 07:12:03       39 阅读
  9. 【threejs】计算矩阵、网格等总面积

    2024-03-30 07:12:03       46 阅读
  10. spark DataFrame通过JDBC读写数据库(MySQL示例)

    2024-03-30 07:12:03       36 阅读