I have a main Div tag with multiple div tags as below. The child Div tags have no class/id that distinguishes from the other child div tags. Now I want to extract the text value from the 2nd child Div tag. How can i do that?
I want to get the text "Monster in Black". This Div doesnt have an id/name and not sure if this style would be same or change. How would i extract using jSoup?
解决方案
package stackoverflow;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JSoupTest {
public static void main(String[] args) throws IOException {
InputStream in = JSoupTest.class.getResourceAsStream("JSoupTest.txt");
String html = IOUtils.toString(in);
Document doc = Jsoup.parse(html);
Elements divs = doc.select("DIV");
System.out.println(divs);
Element div = divs.get(2);
System.out.println("Monster in Black".equals(div.text()));
}
}
Produces:
This is a monster
Monster in Black
This is a monster
Monster in Black
true