python使用beautifulsoup4库爬取酷狗的付费榜

-------- 已失效-------------------
声明:本文仅为技术交流,请勿用于它处。
小编经常在网上听一些音乐但是有一些网站好多音乐都是付费下载的正好我会点爬虫技术,空闲时间写了一份,会下载到当前目录,只要按照bs4库就好,
安装方法:pip install beautifulsoup4
完整代码如下:双击就能直接运行

from bs4 import BeautifulSoup
import requests
import re
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'
}
url='https://songsearch.kugou.com/song_search_v2?&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1555124510574'
#想要爬取别的网页直接修改这个json数据地址就行
r=requests.get(url,headers=headers)
soup=BeautifulSoup(r.text,'lxml')
title_list=soup.select('.pc_temp_songlist ul li')
hash=re.findall(r',"FileHash":"(.*?)"',r.text)
hash1=re.findall(r',"FileName":"(.*?)"',r.text)
#直接用正则匹配隐藏的数据
print(hash)
print(hash1)
q=0
for url in hash:
    url_a=f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery1910212680783679835_1555073815772&hash={url}&album_id=18784389'
    #这个URL不用修改的
    c=requests.get(url_a,headers=headers)
    a=c.text[40:-3]
    b=re.findall('"play_url":"(.*)","authors":',a)[0]
    b1=re.sub(r"\\",'',b)
    f = requests.get(b1)
    with open(hash1[q]+'.mp3','wb')as d:
        d.write(f.content)
    print(hash1[q])
    q+=1

爬取酷狗的唯一难点就是hash值的获取找了一个多小时才找到,比网易云好点就是自己不用写一个哈希值,酷狗是自己就存在的能找到,网易云是需要函数生成的。
以上所述是小编给大家介绍的python获取酷狗音乐top500的下载地址 MP3格,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

猜你喜欢

转载自blog.csdn.net/weixin_43927238/article/details/89527480