Python 代码错误:函数参数未加引号导致语法错误
在函数 get_song_info 的第一行,参数名 004MvQot3V0O8P 没有加引号,导致语法错误。应该修改为字符串形式的参数名。修改后的函数代码如下:
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}')
现在可以调用 get_song_info 函数并传入正确的参数值来获取歌曲信息。例如:
get_song_info("004MvQot3V0O8P")
原文地址: http://www.cveoy.top/t/topic/pMqP 著作权归作者所有。请勿转载和采集!