简体   繁体   中英

Jsoup - Select the value from href attribute

The html code (not from my website, so I cant change it) looks like this:

<div id="resulttable"> 
 <div class="dirlist"> 
  <div class="stationcol" style="width:428px;"> 
   <a href="http://whatever.com?id=xxx" title="Whatever" class="playbutton playimage" name="whatever" id="105867"></a> 
   <div class="videoBody"> 
    <div class="gridModule"> 
     <div class="surrogate"> 
      <div id="thumbnail105867" class="thumbnail"> 
       <a class="playbutton clickabletitle" name="whatever" id="105867" title="Whatever" href="http://whatever.com?id=xxx"> Bla </a>
</div></div></div></div></div></div></div>

Here is my Code:

Document doc = Jsoup.parse(result);
Elements hrefs = doc.select("div.stationcol a[href]");
StringBuilder links = new StringBuilder();

for (Element href : hrefs) {
    links.append(href.text());
}

String httplinks = links.toString();
System.out.println("TEST: " + httplinks);

The output looks like:

I/System.out(10451): Link1http://www.whatever.c...Link2http://www.test.c...

What I really need is an ArrayList that contains the Urls and maybe one separate ArrayList that contains the Titles.

Can anyone help me please?

Do you mean something like this?

ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> urls = new ArrayList<String>();

Document doc = Jsoup.parse(result);
Elements links = doc.select("div.stationcol > a[href]");

for (Element e : links) {
    titles.add(e.attr("title"));
    urls.add(e.attr("href"));
}

System.out.println(titles);
System.out.println(urls);

This will output the contents of both ArrayLists in your sample code, eg:

[Whatever]
[http://whatever.com?id=xxx]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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