简体   繁体   English

Android:使用JSoup从HTML检索多个元素

[英]Android : Retrieve multiples Elements from Html using JSoup

I want to retrieve a title from a div , a start hour and an end hour all of that from a big div called day and inside another div called event 我想从一个div检索一个title ,一个start hour和一个end hour ,从一个名为day的大div以及另一个名为event div内检索所有title

I need to had these items to a list but right now i'am stuck here because it can't retrieve my 3 elements . 我需要将这些items放到list但是现在我被困在这里,因为它无法检索我的3 elements

Document doc = Jsoup.connect("http://terry.gonguet.com/cal/?g=tp11").get();
Elements days = doc.select("div[class=day]");
Elements event = doc.select("div[class=event]");
for(Element day : days)
{
    System.out.println(" : " + day.text());
    for(Element ev : event)
    {

        Element title = ev.select("div[class=title]").first();
        Element starthour = ev.select("div[class=bub right top]").first();
        Element endhour = ev.select("div[class=bub right bottom]").first();
        System.out.println(title.text()+starthour.text()+endhour.text());

    }
}

None of there is no div in that document which have only day as class argument. 还有无是文件,其中只有在不派息day为类参数。 They all have day class combined with another class which prevents div[class=day] from finding such div. 他们都将day类与另一个类组合在一起,这会阻止div[class=day]查找此类div。 Same problem applies to div[class=event] selector. 同样的问题也适用于div[class=event]选择器。

To solve it use CSS query syntax in which . 要解决此问题,请使用CSS查询语法. operator is used to describe class attribute 运算符用于描述类属性
(hint: if you want to select element which has few classes you can use element.class1.class2 ). (提示:如果要选择具有很少类的元素,则可以使用element.class1.class2 )。

So instead of 所以代替

select("div[class=day]");
select("div[class=event]");

use 采用

select("div.day");
select("div.event");

Also instead of 也代替

ev.select("div[class=bub right top]");
ev.select("div[class=bub right bottom]");

you could try using 你可以尝试使用

ev.select("div.bub.right.top");
ev.select("div.bub.right.bottom]");

This will allow you to find div which has all these classes (even if they are not in same order or there are more classes then mentioned in selector). 这将使您能够找到具有所有这些类的div(即使它们的顺序不相同或选择器中提到的类更多)。

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

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