python爬虫酷狗
时间: 2023-12-02 15:03:53 浏览: 101
Python (非付费)酷狗音乐爬取+UI界面
以下是一个简单的Python爬虫酷狗音乐的例子:
```python
import requests
from bs4 import BeautifulSoup
# 获取歌曲下载链接和歌曲名字
def get_music_url_and_name(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
music_url = soup.find('audio')['src']
music_name = soup.find('div', class_='detail').find('h1').text
return music_url, music_name
# 下载歌曲
def download_music(url, name):
print('正在下载音乐......')
with open(f'{name}.mp3', 'wb') as f:
f.write(requests.get(url).content)
print('音乐下载完毕')
# 爬虫主函数
def spider_kugou_music(url):
music_url, music_name = get_music_url_and_name(url)
download_music(music_url, music_name)
# 测试
if __name__ == '__main__':
url = 'https://www.kugou.com/song/2x9l7d3.html'
spider_kugou_music(url)
```
阅读全文