Python 爬虫示例:如何爬取 QQ 音乐歌曲信息
以下是一个简单的示例,展示如何使用 Python 编写一个爬取 QQ 音乐的爬虫。
import requests
from bs4 import BeautifulSoup
# 定义爬取函数
def crawl_qq_music(url):
    # 发起HTTP GET请求
    response = requests.get(url)
    
    # 解析HTML页面
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 找到所有歌曲信息的元素
    song_elements = soup.find_all('li', class_='songlist__item')
    
    # 遍历每个歌曲信息元素并提取数据
    for song_element in song_elements:
        # 获取歌曲名和歌手名
        song_name = song_element.find('div', class_='songlist__songname').text.strip()
        artist_name = song_element.find('div', class_='songlist__artist').text.strip()
        
        # 打印歌曲信息
        print(f'Song: {song_name} - Artist: {artist_name}')
# 调用爬取函数
crawl_qq_music('https://y.qq.com/portal/playlist.html')
请注意,上述代码使用了第三方库requests和BeautifulSoup来发起HTTP请求和解析HTML页面。在运行之前,您需要使用命令pip install requests和pip install beautifulsoup4安装这两个库。
希望这个示例能够满足您的需求。如果您有任何疑问或需要进一步的帮助,请随时告诉我!
原文地址: https://www.cveoy.top/t/topic/o1e 著作权归作者所有。请勿转载和采集!