python写一个爬取歌曲程序
以下是一个简单的Python爬取歌曲的程序示例:
import requests
from bs4 import BeautifulSoup
def crawl_songs(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
song_list = soup.find_all('div', class_='song')
for song in song_list:
title = song.find('span', class_='title').text.strip()
artist = song.find('span', class_='artist').text.strip()
print(f"歌曲: {title}\n歌手: {artist}\n")
if __name__ == '__main__':
url = 'http://example.com/songs' # 替换为实际的歌曲网页URL
crawl_songs(url)
这个示例使用了requests库发送HTTP请求,并使用BeautifulSoup库解析HTML响应。在crawl_songs函数中,我们首先发送GET请求获取网页内容,然后使用BeautifulSoup解析HTML。接下来,我们使用find_all方法查找所有包含歌曲信息的div元素,然后使用find方法分别获取歌曲的标题和艺术家信息。最后,我们打印出每首歌曲的标题和艺术家信息。
请注意,上述示例中的URL需要替换为实际的歌曲网页的URL。另外,爬取网页内容可能涉及到一些法律和道德问题,请确保你在合法和合适的情况下进行爬取
原文地址: https://www.cveoy.top/t/topic/hU2j 著作权归作者所有。请勿转载和采集!