爬取酷狗音乐python代码
时间: 2023-05-19 15:02:18 浏览: 328
以下是一个简单的爬取酷狗音乐的 Python 代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.kugou.com/yy/html/search.html#searchType=song&searchKeyWord='
keyword = '周杰伦'
response = requests.get(url + keyword)
soup = BeautifulSoup(response.text, 'html.parser')
songs = soup.select('.song_name > a')
for song in songs:
print(song.text)
```
这个代码可以根据关键字爬取酷狗音乐中的歌曲名称。
相关问题
python爬取酷狗音乐完整代码
以下是Python爬取酷狗音乐的完整代码:
```python
import requests
import json
def get_music_url(keyword):
search_url = f'http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword={keyword}&page=1&pagesize=1'
response = requests.get(search_url)
json_data = json.loads(response.text)
song_url = json_data['data']['lists'][0]['play_url']
return song_url
def download_music(url, name):
print('正在下载音乐......')
with open(f'{name}.mp3', 'wb') as f:
f.write(requests.get(url).content)
print('音乐下载完毕')
keyword = input('请输入要搜索的音乐关键字:')
url = get_music_url(keyword)
name = input('请输入要保存的音乐名称:')
download_music(url, name)
```
python爬取酷狗音乐源码_python爬虫教程:爬取酷狗音乐
您可以使用 Python 的 requests 库和 BeautifulSoup 库来爬取酷狗音乐的数据。下面是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
def get_song_list(keyword):
url = f'https://www.kugou.com/yy/html/search.html#searchType=song&searchKeyWord={keyword}'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
song_list = []
songs = soup.select('.songName')
for song in songs:
song_name = song.get('title')
song_list.append(song_name)
return song_list
# 使用示例
keyword = '周杰伦'
song_list = get_song_list(keyword)
for song_name in song_list:
print(song_name)
```
这段代码会通过关键字搜索酷狗音乐,并返回匹配的歌曲列表。您可以根据自己的需求进一步提取歌曲的其他信息或下载链接。请注意,爬取网站数据需要遵守网站的相关规定,不要过度请求或滥用。
阅读全文