Turgut Tak
Turgut Tak

Reputation: 43

JSoup can not parse a website

I am trying to get some data from a website. I make copy paste from my old program. But its not working. My code is below.

import java.io.IOException;
import javax.swing.JOptionPane;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class Veri {

    public static void main(String[] args) {

        Veri();

    }

    public static void Veri() {

        try {

            String url = "https://www.isyatirim.com.tr/tr-tr/analiz/hisse/Sayfalar/default.aspx";

            Response res = Jsoup.connect(url).timeout(6000).execute();

            Document doc = res.parse();
            Element ele = doc.select("table[class=dataTable hover nowrap excelexport data-tables no-footer]").first();

            for (int i = 0; i < 100; i++) {

                System.out.println(ele.select("td").iterator().next().text());

            }

        } catch (IOException c) {

            JOptionPane.showMessageDialog(null, "Veriler Alınırken Bir Harta Oluştu!");
            c.printStackTrace();
        }

    }

}

I got the below error

Exception in thread "main" java.lang.NullPointerException at Veri.Veri(Veri.java:37) at Veri.main(Veri.java:20)

Upvotes: 1

Views: 566

Answers (1)

Gerard Rozsavolgyi
Gerard Rozsavolgyi

Reputation: 5064

The page has probably changed a little bit since you last used your program. Try this:

import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Veri {

    public static void main(String[] args) {

        Veri();

    }

    public static void Veri() {

        try {

            String url = "https://www.isyatirim.com.tr/tr-tr/analiz/hisse/Sayfalar/default.aspx";

            Response res = Jsoup.connect(url).timeout(6000).execute();

            Document doc = res.parse();
            Element ele = doc.select("table[class=dataTable hover nowrap excelexport]").first();
            Elements lines = ele.select("tr");
            for (Element elt : lines) {
                System.out.println(elt.text());
                System.out.println("------------------------");
            }

        } catch (IOException c) {

            JOptionPane.showMessageDialog(null, "Veriler Alınırken Bir Harta Oluştu!");
            c.printStackTrace();
        }

    }

}

I think you get all the information needed this way.

Upvotes: 1

Related Questions