Java Web Scraping wth Jaunt Library

2.4k Views Asked by At

I'm facing a problem with Java web scraping. This is the website: (http://www.bbc.com/sport/football/teams/liverpool)

From here I want to scrape the Headlines data.

Note: I'm using Jaunt Library.

public class News extends JFrame
{
    private String title;
    private JLabel labelText2;

    News()
    {

        setSize(800, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        labelText2 = new JLabel();
        setLayout(null);
        labelText2.setBounds(10, 10, 550, 20);
        add(labelText2);
    }

    public void Club_News()
    {
        try {

            UserAgent userAgent = new UserAgent();
            userAgent.visit("http://www.bbc.com/sport/football/teams/liverpool");
            String div = userAgent.doc.findFirst(
                    "<div id=\"more-headlines\" class=\"mod mod-separator\">").innerHTML();
            labelText2.setText("Latest News Headlines: " + div);
        } catch (JauntException e) {
            System.err.println(e);
        }
    }
}
1

There are 1 best solutions below

0
On

You need to call your Club_News() method. You can make it the last statement in your constructor:

add(labelText2); // existing last line
Club_News(); // new method call

To make errors obvious, add a line to your catch block at the end of Club_News():

System.err.println(e); // existing error handling
labelText2.setText(e.toString()); // new hint