gpt4 book ai didi

java - 如何从HTML获取链接, `doc.select`的正确用法

转载 作者:行者123 更新时间:2023-11-30 01:58:09 25 4
gpt4 key购买 nike

我想从 HTML 代码中获取链接。此页面的代码:https://www.valorebooks.com/books/fiction/fantasy

 <div class="sub_bar sub_bar_no_pointer"> 
<span class="showing">Showing 1 - 50 of 28705 - Browse More Fantasy Books for Sale</span>
<div class="paginator" id="pg">
<a href="/books/fiction/fantasy" class="active">1</a>
<a href="/books/fiction/fantasy?page=2">2</a>
<a href="/books/fiction/fantasy?page=3">3</a>
<a href="/books/fiction/fantasy?page=4">4</a>
<a href="/books/fiction/fantasy?page=5">5</a>
<span class="paginatorText">...</span>
<a href="/books/fiction/fantasy?page=575">575</a>
<span class="paginatorText">|</span>
<a href="/books/fiction/fantasy?page=2" class="spriteButton arrow next icon-right-open"></a>
</div>
</div>

我找到了如何获取链接的示例,但我不确定应该在 doc.select("div.paginator"); 中编写什么。正确与否,也许我应该用另一种方式来写。

    Elements myLink = doc.select("div.paginator");      

int number = 0;
for (Element links : myLink) {
Elements a = myLink.select("a[href]"); // get links
number = Integer.parseInt(a.get(a.size() - 2).text());
}
for (int i = 0; i < 20; i++) {
getData(url + i);
}
}

最佳答案

目前尚不清楚您想要实现什么目标。

要打印所有链接的字符串,您可以这样做:

System.out.println(doc.select("div.paginator").select("a[href]").toString());

对于链接的 Elements 数组:

Elements theLinks = doc.select("div.paginator").select("a[href]");

关于java - 如何从HTML获取链接, `doc.select`的正确用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53715223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com