【问题标题】:Java Multi Link Checker Spider - improvements neededJava Multi Link Checker Spider - 需要改进
【发布时间】:2010-11-17 03:37:23
【问题描述】:

我有以下工作代码(在这里和那里进行了更改,以便您在复制和粘贴时使用您的大脑)。我想对其进行改进,以便它检测到所有无效页面,包括待售域名。它的工作效率约为 89%。如果你看到任何我可以通过使用额外的现有库或一些很棒的调整来改进的东西。

 List all = linkService.getAllLinks();
    notValidLinks = new LinkedList();
    final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(39867);
    int poolSize = 90;
    int maxPoolSize = 100;
    long keepAliveTime = 40;
    ThreadPoolExecutor tpe = new ThreadPoolExecutor(poolSize, maxPoolSize,
            keepAliveTime, TimeUnit.SECONDS, queue);

    for (link : all) {
       Thread task = new CheckSite(link);
       tpe.execute(task);
       System.out.println("Task count:" + queue.size());
    }

class CheckSite extends Thread {
    Link link;

    CheckSite(Link link) {
        this.link = link;
    }

    public void run() {
        boolean notValid = false;
        try {
            log.info(link.getLink() + " " + link.getId());
            URL u = new URL(link.getLink());
            HttpURLConnection huc = (HttpURLConnection) u.openConnection();
            HttpURLConnection.setFollowRedirects(false);
            huc.setConnectTimeout(40000);
            huc.setRequestMethod("GET");
            huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");

            huc.connect();
            int code = huc.getResponseCode();

            if (code != HttpURLConnection.HTTP_OK
                    && code != HttpURLConnection.HTTP_MOVED_PERM
                    && code != HttpURLConnection.HTTP_MOVED_TEMP ){
                notValid = true;
                log.info("Invalid code: " + code + " - " + link.getLink());
            }
            if (code == HttpURLConnection.HTTP_MOVED_PERM) {
                log.info(link.getLink() + " Perm move");
            }
            if (code == HttpURLConnection.HTTP_MOVED_TEMP) {
                log.info(link.getLink() + " Temp move");
            }

            try {
                if (!notValid) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(huc.getInputStream()));
                    StringBuilder stringBuilder = new StringBuilder();

                    String line;
                    while ((line = reader.readLine()) != null) {
                        stringBuilder.append(line);
                    }

                    notValid = StringUtils.containsIgnoreCase(Jsoup.parse(stringBuilder.toString()).text(), "Related Searches");

                }
            } catch (Exception e) {
                   log.error(e.getMessage());
            }

            huc.disconnect();
        } catch (MalformedURLException me) {
            log.info("Malformed URL:" + link.getLink());
            notValid = true;
        } catch (IOException e) {
            log.info("Refused connection | Does not exist:" + link.getLink());
            notValid = true;
        }
        if (notValid) {
            link.setApproved(false);
            link.setDateApproved(null);
            notValidLinks.add(linkService.save(link));

        }
        log.debug("URL Finieshed!");
    }
}

【问题讨论】:

    标签: java performance testing web-crawler


    【解决方案1】:

    查看Bloom Filter[wiki]。这将帮助您进行快速且内存高效的查找。布隆过滤器的问题是它会误报,即。对于不存在的东西,它会告诉真。但如果布隆过滤器说假,那肯定是假的。

    【讨论】:

      【解决方案2】:

      我想对其进行改进,以便它能检测到所有无效的网页包括待售域名

      我怀疑突出显示的部分是不切实际的。蜘蛛应该如何判断域名正在出售?

      跟进

      @Mat Banik 建议寻找特定短语或检查 DNS 记录作为可能的解决方案。

      • 检查特定短语的启发式算法会给出误报和误报。

      • 检查 DNS 记录很棘手,在 Java 中也很棘手。您可以对 URL 的主机名部分进行简单的 DNS 查找,并根据已知的 DNS 停放站点 IP 列表检查生成的 IP 地址。但这并不能告诉您原始主机名是否真的在出售。它可能是托管在同一基础架构上的真实网站……或者是非出售的停放域名。

      但我想,如果您准备接受一些误报和误报,那么尝试过滤掉待售域名是可行的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-17
        • 1970-01-01
        • 2014-03-02
        • 1970-01-01
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 2016-07-03
        相关资源
        最近更新 更多