繁体   English   中英

如何使用Jsoup将网页的链接存储在集合中?

[英]How do I store the links of a webpage in a set using Jsoup?

我正在尝试将网页的HTML链接存储在一组中。 (因为集合不允许重复的字符串?)

然后通过这些链接解析HTML

然后将解析的链接存储在另一个集中。

到目前为止,我所得到的是:

    public static void main(String[] args) throws IOException {

    Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Matrix_(mathematics)").get();

    Elements links = doc.select("body a");

    System.out.println(links);

您可以通过以下方式遍历元素的links

for (Element e : links) {
    // called for every element, add them to a set if you wish
}

由于Elements类实现接口java.util.List<E> ,因此有一种简单的方法可以将链接的集合转换为没有重复的集合。

这应该工作:

Set<Element> linkSet = new HashSet<Element>(links);

参考文献:

JavaDocs HashSet - java.util.HashSet

Jsoup文档Elements -org.jsoup.select.Elements

暂无
暂无

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

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