JSoup - исключить ссылки - PullRequest
0 голосов
/ 28 июня 2018

Я пытаюсь исключить список ссылок, которые я не хочу сканировать.

Я не смог найти ничего полезного в документации, которая пропускает запрошенные пользователем 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<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? Я думаю о чем-то похожем на второй блок кода, который я написал, но я открыт для идей или исправлений.

1 Ответ

0 голосов
/ 28 июня 2018

Вы можете начать с выбора и удаления всех нежелательных ссылок. Затем вы можете обработать документ без каких-либо проверок.

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>
...