JSoup无法解析网站

时间:2017-11-13 19:48:48

标签: java eclipse parsing jsoup

我想从网站上获取一些数据。我从旧程序中复制粘贴。但它不起作用。我的代码如下。

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();
        }

    }

}

我收到以下错误

  

线程“main”中的异常java.lang.NullPointerException at   Veri.Veri(Veri.java:37)在Veri.main(Veri.java:20)

1 个答案:

答案 0 :(得分:1)

自您上次使用程序以来,该页面可能已经发生了一些变化。 试试这个:

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();
        }

    }

}

我认为你可以通过这种方式获得所需的所有信息。