python写一个可以爬取音乐的代码
时间: 2023-09-26 12:06:49 浏览: 197
### 回答1:
这是一段爬取音乐的简单 Python 代码:
```python
import requests
from bs4 import BeautifulSoup
# 请求网页
url = "https://music.163.com/playlist?id=3778678"
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'}
res = requests.get(url, headers=headers)
# 解析网页
soup = BeautifulSoup(res.text, 'html.parser')
# 查找音乐信息
music_list = soup.select('.f-hide a')
for music in music_list:
print(music.getText())
```
此代码爬取的是网易云音乐中某个歌单的信息,使用了requests库和beautifulsoup库,需要在终端中运行。
请注意使用爬虫爬取网站的数据时需要遵循网站的相关协议和规定。
### 回答2:
以下是一个使用Python编写的爬取音乐的代码示例:
```python
import requests
from bs4 import BeautifulSoup
def get_music_list(keyword):
url = f"https://www.example.com/search?keywords={keyword}"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
music_list = []
results = soup.find_all('div', {'class': 'music-item'})
for result in results:
title = result.find('h2', {'class': 'title'}).text.strip()
artist = result.find('p', {'class': 'artist'}).text.strip()
music_list.append({'title': title, 'artist': artist})
return music_list
def download_music(url, file_path):
response = requests.get(url)
with open(file_path, 'wb') as file:
file.write(response.content)
if __name__ == "__main__":
keyword = input("请输入要搜索的音乐关键字:")
music_list = get_music_list(keyword)
print(f"共找到{len(music_list)}首符合条件的音乐:")
for index, music in enumerate(music_list):
print(f"{index + 1}. 标题:{music['title']},艺术家:{music['artist']}")
# 假设下载链接在music字典的'download_url'键中保存
download_url = music['download_url']
file_path = f"music{index + 1}.mp3"
download_music(download_url, file_path)
print(f"音乐已下载保存为:{file_path}")
```
这段代码通过向音乐搜索网站发送GET请求,获取搜索结果页面的HTML内容,并使用BeautifulSoup解析HTML,提取出音乐的标题和艺术家信息。然后,根据用户输入的关键字,获取音乐列表。接下来,代码迭代音乐列表,并下载每首歌曲的音乐文件到本地。在代码中,假设下载链接保存在音乐字典的`download_url`键中,下载的音乐文件将保存为`music1.mp3`、`music2.mp3`等。
### 回答3:
import requests
from bs4 import BeautifulSoup
# 定义爬取音乐的函数
def crawl_music(keyword):
url = f"https://music.example.com/search?keyword={keyword}" # 替换为实际的音乐网站链接
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"
} # 添加User-Agent以伪装浏览器请求
response = requests.get(url, headers=headers) # 发送GET请求
soup = BeautifulSoup(response.text, "html.parser") # 解析网页
music_list = soup.find_all("a", class_="music") # 根据实际网页结构找到音乐链接标签
for music in music_list:
music_name = music.text # 获取音乐名称
music_url = music["href"] # 获取音乐链接
print("音乐名称:" + music_name)
print("音乐链接:" + music_url)
print("--------")
# 调用函数进行音乐爬取
crawl_music("周杰伦") # 以"周杰伦"为关键词爬取音乐链接
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)