import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import org.jsoup.nodes.TextNode; import org.jsoup.select.Elements; import org.junit.jupiter.api.Test; import java.util.regex.Pattern; import static org.junit.jupiter.api.Assertions.assertNotNull; public class LyricsFetch { Pattern pattern = Pattern.compile("(?i)\\[(verse.*)|(chorus.*)|(bridge.*)|(outro.*)|(intro.*)]"); @Test public void testFetchLyrics() throws Exception { Document doc = Jsoup.connect("https://genius.com/Neuro-sama-life-lyrics") .userAgent("Mozilla/5.0") .get(); Elements containers = doc.select("div[data-lyrics-container=true]"); StringBuilder lyrics = new StringBuilder(); for (Element container : containers) { for(Node n : container.childNodes()) { if(n instanceof Element e) { if (e.attribute("data-exclude-from-selection") != null && e.attr("data-exclude-from-selection").equals("true")) { continue; } else if(e.tagName().equalsIgnoreCase("br")) { lyrics.append("\n"); } else { System.out.println(container.tagName()); String s = e.text(); lyrics.append(s.trim()); } } else if(n instanceof TextNode tn) { String s = tn.text(); if (!s.isBlank()) { lyrics.append(s.trim()); } } } } System.out.println(lyrics.toString()); assertNotNull(lyrics.toString()); } }