Jsoup (Find Element)

徘徊边缘 提交于 2020-01-14 05:32:06

问题


Help solve the problem, it is necessary to pull some data from Wikipedia, I'll show them in the picture below:

In the page code, these data are here:

How to get this data? to do this is by using jsoup.

I tried to do it like this:

 System.out.println(doc.select("div.mw-body-content > p ").first().text());

But the problem is that it so happens that this is not the first

in code, and the second is for something:


回答1:


  1. Get the parent div by its ID (which should be unique):

    Elements parent = doc.select("div#mw-body-content");
    
  2. Get all p tags in this element (including the second one you would like to have):

    Elements paragraphs = parent.getElementsByTag("p");
    
  3. Take the second of it:

    String text = paragraphs.get(1).text();
    


来源:https://stackoverflow.com/questions/23978520/jsoup-find-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!