以下是一个简单的用Python爬取歌曲的例子,使用了BeautifulSoup和requests库:

import requests
from bs4 import BeautifulSoup

# 输入歌曲名
song_name = input("请输入歌曲名:")

# 搜索url
search_url = f"https://www.xiami.com/search?key={song_name}"

# 发送请求
response = requests.get(search_url)

# 解析html
soup = BeautifulSoup(response.text, "html.parser")

# 找到第一个搜索结果
search_result = soup.find(class_="lib_song")

# 获取歌曲链接
song_url = search_result.find("a")["href"]

# 发送请求
response = requests.get(song_url)

# 解析html
soup = BeautifulSoup(response.text, "html.parser")

# 找到歌曲名称和歌手名称
song_title = soup.find(class_="detail_header_title").text.strip()
song_artist = soup.find(class_="detail_header_artist").text.strip()

# 打印歌曲信息
print(f"歌曲名称:{song_title}")
print(f"歌手名称:{song_artist}")

这个例子中首先输入要搜索的歌曲名,然后发送搜索请求,解析搜索结果页面,找到第一个搜索结果的链接,发送该链接的请求,解析歌曲页面,找到歌曲名称和歌手名称,最后打印出来。

写一个用python爬取歌曲的代码

原文地址: https://www.cveoy.top/t/topic/Yui 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录