Given this HTML:
<html>
<head></head>
<body>
<p>
"Text"
<br>
"Some more Text"
<br>
"Even more text"
</p>
</body>
</html>
I'm trying to get the text inside the <p>
tags with §Element description = document.select(______)`. How can I get this text? I was able to do it with a page that didn't have a body but I'm not sure how to get past the body tags. Thanks.
Yo can use the selector: p
to extract all <p>
elements and use the element accessor: text()
to read the text from within each <p>
element.
Here's an example using the HTML provided in your question:
@Test
public void canGetTextFromAPElement() {
String html = "<html> \n" +
" <head></head>\n" +
" <body>\n" +
" <p>\n" +
" \"Text\"\n" +
" <br>\n" +
" \"Some more Text\"\n" +
" <br> \n" +
" \"Even more text\"\n" +
" </p>\n" +
" </body>\n" +
"</html>";
Document doc = Jsoup.parse(html);
Elements elements = doc.select("p");
assertThat(elements.size(), is(1));
assertThat(elements.get(0).text(), is("\"Text\" \"Some more Text\" \"Even more text\""));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.