简体   繁体   中英

Jsoup to get data on <b> block

I'm new to JSoup on Java and would like to enquire few questions. Given the HTML code of the page I would like to get is this

<td width="70%" class="row1">
<b>4</b>
<br />( 0 posts per day / 0.00% of total forum posts )
</td>

My question here is I want to get the data 4 but the output I get is 4 ( 0 posts per day / 0.00% of total forum posts )

Here is my Java code

Iterator <Element> element = totalPost.select("td[width=70%][class=row1]").iterator();

System.out.println(element.next().text());

Sorry if my question is not clear enough.

This indeed would not work for me if there is no <table> after adding <table> tag it works for me:

<table>
<td width="70%" class="row1">
<b>4</b>
<br />( 0 posts per day / 0.00% of total forum posts )
</td>
</table>

You may try checking if your HTML is being parsed well by adding printing the element or the whole document:

System.out.println("totalPost:" + totalPost.html());
System.out.println("doc:" + doc.html());

Here's an example:

final String html = "<td width=\"70%\" class=\"row1\">\n"
        + "<b>4</b>\n"
        + "<br />( 0 posts per day / 0.00% of total forum posts )\n"
        + "</td>";

Document doc = Parser.xmlParser().parseInput(html, ""); // Alternative: Jsoup.parse(...) or connect to a website
Element bTag = doc.select("td > b").first();

System.out.println(bTag.text());

This will print 4 .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM