如何使用Jsoup从链接中提取href? [英] How to extract href from links using Jsoup?

查看:165
本文介绍了如何使用Jsoup从链接中提取href?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得这样的链接:

index.php?limitstart=0&picno=0&gallery_key=92
index.php?limitstart=0&picno=1&gallery_key=92
index.php?limitstart=0&picno=2&gallery_key=92

来自此html使用Jsoup:

from this html using Jsoup :

<tr> 
<td style="padding: 8px;"><a onclick="redx_gallery_showImage(0);return false;" href="/module/gallery/index.php?limitstart=0&amp;picno=0&amp;gallery_key=92"><img width="90" height="90"  style='border: 1px #BAB9AF solid'   src='/redx_tools/mb_image.php/cid.077117104075119048121090118052048061/gid.10/pyrit_club_2_buche.jpg' border='1'    alt=''/></a></td> 
    <td style="padding: 8px;"><a onclick="redx_gallery_showImage(1);return false;" href="/module/gallery/index.php?limitstart=0&amp;picno=1&amp;gallery_key=92"><img width="90" height="90"  style='border: 1px #BAB9AF solid'   src='/redx_tools/mb_image.php/cid.085057100083102116053082117052115061/gid.10/pyrit_club_2_weiss.jpg' border='1'    alt=''/></a></td> 
    <td style="padding: 8px;"><a onclick="redx_gallery_showImage(2);return false;" href="/module/gallery/index.php?limitstart=0&amp;picno=2&amp;gallery_key=92"><img width="90" height="90"  style='border: 1px #BAB9AF solid'   src='/redx_tools/mb_image.php/cid.120068065087108097121088078055048061/gid.10/pyrit_club_2_wei_2.jpg' border='1'    alt=''/></a></td> 
</tr> 

任何想法?谢谢

推荐答案

您需要知道公共容器的 id 元素,以便您可以在一个CSS选择中获取它们。根据来源,它是< div id =redx_gallery_thumb_list>

You need to know the id of a common container element so that you can get them all in a single CSS select. According to the source it's the <div id="redx_gallery_thumb_list">.

所以,这应该做:

Elements links = document.select("#redx_gallery_thumb_list a");

for (Element link : links) {
    String href = link.attr("href");

    // Or if you want to have absolute URL instead, so that you can leech them.
    String absUrl = link.absUrl("href");

    // ...
}

这篇关于如何使用Jsoup从链接中提取href?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆