主页 M

python应用BeautifulSoup爬虫实现下载网易音乐

2020-03-14 网页编程网 网页编程网
# -*- coding: utf-8 -*-
"""
访问:https://music.163.com/#/playlist?id=3136952023
播放文件 https://m801.music.126.net/20200314151310/ebbb24f725b01e47839ad38a944fa296/jdyyaac/0153/075e/545b/6946f266960496d2a327c1451c021ee3.m4a

没有规律
抓包求fidder抓包真正的跳转地址:
http://music.163.com/song/media/outer/url?id=1398663411
二次请求
"""
import requests
#模拟浏览器版本pip install fake-useragent
from fake_useragent import UserAgent
import re#正则
from bs4 import BeautifulSoup
from urllib import request
headers={
        #'User-Agent':UserAgent().random
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4068.4 Safari/537.36'
        }
def get_respsonse(url,headers=headers):
    try:
        reponse=requests.get(url,headers=headers)
        if reponse.status_code==200:
            return reponse
    except:
        return None


def get_song_name(songid):
    url='https://music.163.com/song?id={}'.format(songid)
    text=get_respsonse(url,headers=headers).text
    #方法一re.findall('<title>(.*?)</title>',text)
    soup=BeautifulSoup(text,'lxml')
    title=soup.title.text
    return title.split('-')[0].strip()

if __name__ == '__main__':
    songid=input('输入下载的id*85621:')
    title=get_song_name(songid)
    #原始下载
    down_url='http://music.163.com/song/media/outer/url?id={}'.format(songid)
    #重定向
    red_down_url=get_respsonse(down_url,headers=headers).url
    #下载
    request.urlretrieve(red_down_url,title+'.mp3')
    print('over')
阅读原文
阅读 14367
123 显示电脑版