首页 > 解决方案 > 用beautifulsoup从天才歌词中获取歌曲歌词│python 3.8

问题描述

我正在尝试使用 beautifulsoup 从天才歌词中获取歌曲的歌词,但是在尝试打印歌词时,我没有得到任何输出。这是我的代码:

import requests 
from bs4 import BeautifulSoup
songURL = requests.get("https://genius.com/Marshmello-and-bastille-happier-lyrics")
song = songURL.content
soup = BeautifulSoup(song, 'lxml')
lyrics = soup.find_all("section")
for lyr in lyrics:
    for lyr1 in lyrics.select("p"):
        print(lyr1.text)      

为什么这不起作用,有人可以调查一下,因为我一直在尝试这样做一段时间。

标签: pythonhtmlpython-3.xbeautifulsouppython-requests

解决方案


似乎服务器返回了页面的两个版本:在一个版本中,标签带有class="song_body-lyrics",在另一个版本中带有class="Lyrics__Container..."

此脚本尝试处理这两种情况:

import requests 
from bs4 import BeautifulSoup

url = 'https://genius.com/Marshmello-and-bastille-happier-lyrics'
soup = BeautifulSoup(requests.get(url).content, 'lxml')

for tag in soup.select('div[class^="Lyrics__Container"], .song_body-lyrics p'):
    t = tag.get_text(strip=True, separator='\n')
    if t:
        print(t)

印刷:

[Intro]
Lately, I've been, I've been thinking
I want you to be happier, I want you to be happier
[Verse 1]

...and so on.

推荐阅读