跳过前5个元素" li"标签

时间:2016-02-03 16:13:51

标签: loops jsoup

我在数据库中工作,我需要从网上检索几个信息。使用Jsoup阅读字体代码页,我想跳过前#5;" li"标签。我正在尝试这个,但它根本不起作用:

                    String letra=editText.getText().toString().toUpperCase();

                    String url="https://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code:_"+letra;

                    try {
                        Document doc = Jsoup.connect(url).get();
                        Elements liTags=doc.select("li");
                        int c=0;

                        for(org.jsoup.nodes.Element ligtag : liTags ){

                            if(c<5) {
                                frase =liTags.text()+" \r\n";
                                tv9.setText(" ");
                                c++;
                            }

                            else
                                frase =liTags.text()+" \r\n";
                                tv9.setText(frase);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

提前致谢!

1 个答案:

答案 0 :(得分:0)

您是否在调试器中查看了代码?我猜不是,因为它有几个严重的错误:

  1. 您在liTags上进行循环,ligtag包含当前元素,但您从不使用此元素。

  2. else block未被大括号括起,只有else的第一个语句部分。

  3. tv9.setText(frase);行看起来对我不好。我不知道tv9是什么类型的,但我想setText方法不会将新输入连接到已经存在的,而是替换tv9对象的文本值。然而,这只是猜测。

  4. 这是一个使用一些CSS魔法来获取相关li标签的工作版本,而不是仅仅省略前5个li元素:

    String letra="F";
    String url="https://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code:_"+letra;
    try {
        Document doc = Jsoup.connect(url).get();
        Elements liTags = doc.select("li:has(b:matches(^"+letra+"))");
    
        for(org.jsoup.nodes.Element liTag : liTags ){
            String frase = liTag.text()+" \r\n";
            System.out.print(frase);
        }
    
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    CSS选择器会选择所有li标记,其中包含b标记,其中包含以您想要的字母开头的文字。

    回复评论的附录

    对于字母&#34; A&#34;我得到以下输出:

    AGAF (AFT) – Afutara Airport – Afutara 
    AGAR (RNA) – Ulawa Airport – Arona, Ulawa Island 
    AGAT (ATD) – Uru Harbour Airport – Atoifi, Malaita 
    AGBA (VEV) – Barakoma Airport – Barakoma 
    AGBT (BPF) – Batuna Airport – Batuna 
    AGEV (GEF) – Geva Airport – Geva 
    AGGA (AKS) – Auki Gwaunaru'u Airport – Auki 
    AGGB (BNY) – Bellona/Anua Airport – Bellona/Anua 
    AGGC (CHY) – Choiseul Bay Airport – Choiseul Bay, Taro Island 
    AGGD (MBU) – Mbambanakira Airport – Mbambanakira 
    AGGE (BAS) – Balalae Airport – Shortland Island 
    AGGF (FRE) – Fera/Maringe Airport – Fera Island, Santa Isabel Island 
    AGGG – Honiara FIR – Honiara, Guadalcanal 
    AGGH (HIR) – Honiara International Airport (formerly Henderson Field) – Honiara, Guadalcanal 
    AGGI – Babanakira Airport – Babanakira 
    AGGJ (AVU) – Avu Avu Airport – Avu Avu 
    AGGK (IRA) – Kirakira Airport – Kirakira 
    AGGL (SCZ) – Santa Cruz/Graciosa Bay/Luova Airport – Santa Cruz/Graciosa Bay/Luova, Santa Cruz Island 
    AGGM (MUA) – Munda Airport – Munda, New Georgia Island 
    AGGN (GZO) – Nusatupe Airport – Gizo Island 
    AGGO (MNY) – Mono Airport – Mono Island 
    AGGP (PRS) – Marau Sound Airport – Marau Sound 
    AGGQ (OTV) – Ontong Java Airport – Ontong Java 
    AGGR (RNL) – Rennell/Tingoa Airport – Rennell/Tingoa, Rennell Island 
    AGGS (EGM) – Seghe Airport – Seghe 
    AGGT (NNB) – Santa Ana Airport – Santa Ana 
    AGGU (RUS) – Marau Airport – Marau 
    AGGV (VAO) – Suavanao Airport – Suavanao 
    AGGY (XYA) – Yandina Airport – Yandina 
    AGIN – Isuna Heliport – Isuna 
    AGKG (KGE) – Kaghau Airport – Kaghau Island 
    AGKU (KUE) – Kukudu Airport – Kukudu 
    AGOK (GTA) – Gatokae Aerodrome – Gatokae 
    AGRC (RIN) – Ringi Cove Airport – Ringi Cove 
    AGRM (RBV) – Ramata Airport – Ramata Island 
    ANAU – Nauru Flight Information Region 
    ANYN (INU) – Nauru International Airport – Yaren 
    AYAA (AMF) – Ama Airport – Ama 
    AYBK (BUA) – Buka Airport – Buka 
    AYCH (CMU) – Chimbu Airport – Kundiawa 
    AYDU (DAU) – Daru Airport – Daru 
    AYGA (GKA) – Goroka Airport – Goroka 
    AYGN (GUR) – Gurney Airport – Alotau 
    AYGR (PNP) – Girua Airport – Popondetta 
    AYHK (HKN) – Hoskins Airport – Kimbe 
    AYIQ (KIE) – Kieta Aropa Airport – Kieta 
    AYKA (LSA) – Losuia Airport – Kiriwina 
    AYKI (UNG) – Kiunga Airport – Kiunga 
    AYKK (KRI) – Kikori Airport – Kikori 
    AYKM (KMA) – Kerema Airport – Kerema 
    AYKT (KZF) – Kaintiba Airport – Kaintiba 
    AYKV (KVG) – Kavieng Airport – Kavieng 
    AYKY – Kunaye Airport – Kunaye 
    AYLA – Lae Airfield – Lae 
    AYMD (MAG) – Madang Airport – Madang 
    AYMH (HGU) – Mount Hagen Airport – Mount Hagen 
    AYMN (MDU) – Mendi Airport – Mendi 
    AYMO (MAS) – Momote Airport – Manus Island 
    AYMR (MXH) – Moro Airport – Moro 
    AYMS (MIS) – Misima Island Airport – Misima 
    AYNZ (LAE) – Lae Nadzab Airport – Lae / Nadzab 
    AYPY (POM) – Port Moresby/Jackson International Airport – Port Moresby 
    AYRB – Rabaul Airport (old) (destroyed 1994) – Rabaul, New Britain 
    AYTA (TIZ) – Tari Airport – Tari 
    AYTB (TBG) – Tabubil Airport – Tabubil 
    AYTK (RAB) – Rabaul Airport (Tokua Airport) – Rabaul / Tokua 
    AYVN (VAI) – Vanimo Airport – Vanimo 
    AYWD (WBM) – Wapenamanda Airport – Wapenamanda 
    AYWK (WWK) – Wewak International Airport – Wewak 
    

    AYNZ和AYPY都是该名单的一部分。