【问题标题】:Check if element exists in fetched URL [closed]检查获取的 URL 中是否存在元素 [关闭]
【发布时间】:2015-06-17 16:55:09
【问题描述】:

我有一个包含 30 个 URL 的页面,我需要单击每个 URL 并检查一个元素是否存在。 目前,这意味着:

$('area').each(function(){
    $(this).attr('target','_blank');
    var _href = $(this).attr("href"); 
    var appID = (window.location.href).split('?')[1];
    $(this).attr("href", _href + '?' + appID);
    $(this).trigger('click');
});

打开了 30 个新标签,我手动浏览它们。

(所有网址都在同一个域中)

如果有一个具有以下逻辑的爬虫,那就太好了:

$('area').each(function(){

 1) get the HREF
 2) follow it
 3) on that new page:
    if($('.element')){
     push the $('area') into array1 
    } else {
     push the $('area') into array2
        }
    });


   4) Display array1 in green
      Display array2 in red

基本上,我想生成一份报告:

X 抓取的页面元素 Y

Z 爬取的页面没有元素 Y

我显然坚持让 Javascript/jQuery 在新打开的选项卡中工作。

我找到了 thisthisthis ,但我不完全确定这是否可行。

这可以用 Javascript/jQuery 完成吗?

我只是要求正确的方向,我会自己做这些步骤。

非常感谢

【问题讨论】:

    标签: javascript jquery python web-crawler window.open


    【解决方案1】:

    我可以建议你使用iframe 来加载页面。

    例如:

    $.each($your-links, function(index, link) {
        var href = $(link).attr("href");
        // your link preprocess logic ...
    
        var $iframe = $("<iframe />").appendTo($("body"));
        $iframe.attr("src", href).on("load", function() {
            var $bodyContent = $iframe.contents().find("body");
            // check iframe content and remove iframe
            $iframe.remove();
        }
    }
    

    但是,我应该说,如果您的爬虫和检查页面具有不同的域,则会出现 CORS 问题。

    我创建了一个简单的项目来展示如何实现这种方法。 您可以下载它here 并在一些本地网络服务器(apache、iis 等)上运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-03
      • 2012-11-20
      • 1970-01-01
      • 2011-09-26
      相关资源
      最近更新 更多