java - JSoup - 排除链接
问题描述
我正在尝试排除我不想抓取的链接列表。
我在跳过用户请求的 url 的文档中找不到任何有用的东西。
但是,我能够这样做:
if(!(link.attr("href").startsWith("https://blog.olark.com") ||
link.attr("href").startsWith("http://www.olark.com")||
link.attr("href").startsWith("https://www.olark.com")||
link.attr("href").startsWith("https://olark.com") ||
link.attr("href").startsWith("http://olark.com"))) {
this.links.add(link.absUrl("href")); //get the absolute url and add it to links list. }
当然这不是一个正确的方法,所以我将链接包装在一个 List 中并尝试循环遍历它 - 但是,它并没有排除单个链接(下面的代码):
List<String> exclude = Arrays.asList("https://blog.olark.com", "http://www.olark.com", "https://www.olark.com", "https://olark.com", "http://olark.com");
for (String string : exclude) {
if(!link.attr("href").startsWith(string)) {
this.links.add(link.absUrl("href")); //get the absolute url and add it to links list.
}
}
所以我的问题是:我如何让它避免一个 url 列表?我正在考虑类似于我编写的第二个代码块的东西,但我愿意接受想法或修复。
解决方案
您可以从选择和删除所有不需要的链接开始。然后,您无需任何检查即可处理您的文档。
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupQuestion51072084 {
public static void main(final String[] args) throws IOException {
Document doc = Jsoup.parse("<a href=\"http://www.olark.com\"></a>" +
"<a href=\"https://www.olark.com\"></a>" +
"<a href=\"https://google.pl\"></a>" +
"<a href=\"http://olark.com/qwerty\"></a>" +
"<a href=\"https://olark.com/asdf\"></a>" +
"<a href=\"https://stackoverflow.com\"></a>");
System.out.println("Document before modifications:\n" + doc);
// select links having "olark.com" in href.
Elements links = doc.select("a[href*=olark.com]");
System.out.println();
System.out.println("Links to remove: " + links);
System.out.println();
// remove them from the document
for (Element link : links) {
link.remove();
}
System.out.println("Document without unwanted links:\n" + doc);
}
}
输出是:
Document before modifications:
<html>
<head></head>
<body>
<a href="http://www.olark.com"></a>
<a href="https://www.olark.com"></a>
<a href="https://google.pl"></a>
<a href="http://olark.com/qwerty"></a>
<a href="https://olark.com/asdf"></a>
<a href="https://stackoverflow.com"></a>
</body>
</html>
Links to remove: <a href="http://www.olark.com"></a>
<a href="https://www.olark.com"></a>
<a href="http://olark.com/qwerty"></a>
<a href="https://olark.com/asdf"></a>
Document without unwanted links:
<html>
<head></head>
<body>
<a href="https://google.pl"></a>
<a href="https://stackoverflow.com"></a>
</body>
</html>