简体   繁体   English

用Jsoup解析表元素

[英]Parsing table elements with Jsoup

I'm trying to parse data from this table . 我试图解析该表中的数据。 Let's say, for example, that I want to parse the second elements from the second row (called SLO). 例如,假设我想解析第二行中的第二个元素(称为SLO)。

在此处输入图片说明

I can see there is a TR inside TR and the SLO word doesn't even have an ID or anything. 我可以看到TR内有一个TR,而SLO单词甚至没有ID或任何东西。 How can I parse this? 我该如何解析?

This is the code: 这是代码:

class Title extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        tw1.setText("Loading...");
    }

    @Override
    protected Void doInBackground(Void... params) {
            try {
                Document doc = Jsoup.connect("https://www.easistent.com/urniki/cc45c5d0d303f954588402a186f5cdba5edb51d6/razredi/16515").get();
                Elements eles = doc.select("");
                title = eles.toString();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        tw1.setText(title);
    }

}

I don't know what to put in the doc.select(""); 我不知道在doc.select(“”);中放什么。 because I've never parsed something like this. 因为我从未解析过这样的东西。 I've only parsed titles of webpages and such. 我只解析了网页标题等。 Could someone help me with this? 有人可以帮我吗?

There is plenty of information there for you to use, for example class names or title attributes. 有很多信息供您使用,例如,类名或标题属性。 The URL you provided won't work for me, and I can't copy paste the HTML from your image so my example will show just the parsing of the span based on its title: 您提供的URL对我不起作用,并且我无法从您的图片中复制粘贴HTML,因此我的示例将仅基于其标题显示跨度的解析:

String html = "<span title='Slovenscina'>SLO</span>";
Document doc = Jsoup.parse(html);
Elements eles = doc.select("span[title=Slovenscina]");
String title = eles.text();
System.out.println(title);

Will output: 将输出:

SLO

This will work in the scope of the other HTML that you provided. 这将在您提供的其他HTML的范围内起作用。 I suggest you read some more about the selector-syntax of Jsoup. 我建议您阅读有关Jsoup的选择器语法的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM