Getting text from a website using JSoup

java, jsoup, parsing, web

Solution

Change:

doc.select("div.mp-tfa");

To:

doc.select("div#mp-tfa");

The better way would to iterate over the `Elements` thus retrieved for the `tag`, `class` or `Element` of your choice, simply put:

Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Main_Page").get();
Elements el = doc.select("div#mp-tfa");
for (Element e : el) {
    System.out.println(e.text());
}

Would give:

The Boulonnais is a heavy draft horse breed from Fr....

Problem

I’m working with JSoup to parse the html website. I want to get the article from (for example) Wikipedia. I would like to get the text from the main page (http://en.wikipedia.org/wiki/Main_Page) from the table “From today’s featured article”. Here’s the code: ``` Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Main_Page”); Elements el = doc.select("div.mp-tfa”); System.out.println(el); ``` The problem is that it doesn’t work properly - it prints out just a blank line. The “From today’s featured article” table is inserted in div class=“mp-tfa”. How to get this text in my java program? Thanks in advance.

Original source