需要下载requests和beautifulsoup模块。

import requests
from bs4 import BeautifulSoup

def get_song_info(song_id):
    url = f'https://y.qq.com/n/yqq/song/{song_id}.html'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    
    song_title = soup.find('h1', class_='data__name_txt').text.strip()
    singer_name = soup.find('span', class_='data__singer_txt').text.strip()
    album_name = soup.find('a', class_='data__album_name_txt').text.strip()
    
    print(f'Song Title: {song_title}')
    print(f'Singer Name: {singer_name}')
    print(f'Album Name: {album_name}')
Python爬取QQ音乐歌曲信息 - 使用requests和BeautifulSoup

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

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