【问题标题】:JSOUP parsing HTML get class inside classJSOUP解析HTML获取类内的类
【发布时间】:2012-09-14 09:56:48
【问题描述】:

我正在开发使用 JSOUP 解析 HTML 的 android 应用程序。

我有 HTML 语法

    <div class='wrapper'>   
<div style='margin:7px;'>
    <div class='box' style='height:595px'>
        <div class='boxtitlebox'>
            <div class='boxtitle'><h4>13 RECENT CHORDS</h4></div><div class='clear'></div>
        </div>

        <div class='listitem'><a href='http://www.chordfrenzy.com/chord/9742/ungu-apa-sih-maumu-kord-lirik-lagu'>           
            <div class='subtitle'>Chord Ungu</div>
            <div class='title'>Apa Sih Maumu</div>
        </a></div>
        <div class='listitem'><a href='http://www.chordfrenzy.com/chord/6826/slank-boneka-tersayang-kord-lirik-lagu'>           
            <div class='subtitle'>Chord Slank</div>
            <div class='title'>Boneka Tersayang</div>
        </a></div>
        <div class='listitem'><a href='http://www.chordfrenzy.com/chord/6751/ari-lasso-rayuan-gombal-kord-lirik-lagu'>          
            <div class='subtitle'>Chord Ari Lasso</div>
            <div class='title'>Rayuan Gombal</div>
        </a></div>
        </div>
</div>
 </div>

现在,我很困惑如何获得上面的每个 ahrefsubtitletitle

我需要它来像这样填充我的数组

String[] link=["http://www.chordfrenzy.com/chord/9742/ungu-apa-sih-maumu-kord-lirik-lagu","http://www.chordfrenzy.com/chord/6826/slank-boneka-tersayang-kord-lirik-lagu","http://www.chordfrenzy.com/chord/6751/ari-lasso-rayuan-gombal-kord-lirik-lagu"];
String[] subtitile=["Chord Ungu","Chord Slank","Chord Ari Lasso"];
String[] title=["Apa Sih Maumu","Boneka Tersayang","Rayuan Gombal"];

有什么想法吗?

【问题讨论】:

    标签: android parsing html-parsing jsoup


    【解决方案1】:

    一般而言,您应该更喜欢Selector API 而不是 DOM (getElementsByX)

    这是一个例子:

    Document doc = Jsoup.parse(html);
    
    
    // Links
    List<String> links = new ArrayList<>();
    
    for( Element element : doc.select("a[href]") )
    {
        links.add(element.attr("href"));
    }
    
    
    // Subtitles
    List<String> subtitles = new ArrayList<>();
    
    for( Element element : doc.select("div[class=subtitle]") )
    {
        subtitles.add(element.text());
    }
    
    
    // Titles
    List<String> titles = new ArrayList<>();
    
    for( Element element : doc.select("div[class=title]") )
    {
        titles.add(element.text());
    }
    

    元素由标签和属性选择,如果标签不同或不相关,您可以删除它们(例如[class=title] 而不是div[class=title])。查看 Selector API(上面的链接)了解更多提示。

    【讨论】:

      【解决方案2】:
       Document document = Jsoup.parse(html);
      
               Elements hrefElements = document.select("div.listitem");
      
               String[] links = new String[hrefElements.size()];
               String[] title = new String[hrefElements.size()];
               String[] subtitle = new String[hrefElements.size()];
      
               for(int i=0;i<hrefElements.size();i++)
               {
                   links[i] = hrefElements.get(i).getElementsByTag("a").attr("href");
                   title[i] = hrefElements.get(i).getElementsByClass("title").text();
                   subtitle[i] = hrefElements.get(i).getElementsByClass("subtitle").text();
               }
      
      
               for(int j=0;j<hrefElements.size();j++)
               {
                   System.out.println("Links: "+links[j]);
                   System.out.println("Title: "+title[j]);
                   System.out.println("SubTitle: "+subtitle[j]);
               }
      

      【讨论】:

        【解决方案3】:

        我认为ArrayList 结构比字符串数组更好

        Elements links = doc.getElementsByClass("listitem");
        Elements subtitles = doc.getElementsByClass("subtitle");
        Elements titles = doc.getElementsByClass("title");
        List<String> link = new ArrayList<String>();
        List<String> subtitile = new ArrayList<String>();
        List<String> title = new ArrayList<String>();
        for (Element e : links) {
            String href = e.getElementsByAttribute("href").first().attr("href");
            link.add(href);
        }
        for (Element e : subtitles) {
            String s = e.text();
            subtitile.add(s);
        }
        for (Element e : titles) {
            String s = e.text();
            title.add(s);
        }
        

        【讨论】:

          猜你喜欢
          • 2011-09-12
          • 1970-01-01
          • 1970-01-01
          • 2011-12-11
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 2013-12-11
          • 1970-01-01
          相关资源
          最近更新 更多