我正在尝试使用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)
为什么这不起作用,请问有人,对此我已经尝试了一段时间了。
服务器似乎返回了该页面的两个版本:在一个版本中,带有的标签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.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句