简体   繁体   English

Jsoup - 从href属性中选择值

[英]Jsoup - Select the value from href attribute

The html code (not from my website, so I cant change it) looks like this: HTML代码(不是来自我的网站,所以我无法改变它)看起来像这样:

<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. 我真正需要的是一个包含Urls的ArrayList ,也可能是一个包含Titles的独立ArrayList

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: 这将输出示例代码中两个ArrayLists的内容,例如:

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

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

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