提取多个链接并使用JQuery在控制台中单击 [英] Extract multiple link and click in console using JQuery

查看:63
本文介绍了提取多个链接并使用JQuery在控制台中单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个Jquery提取很陌生.我现在的问题是在Chrome控制台中通过代码自动点击提取的链接.我使用下面的代码在href中获取链接,但click(),trigger('click')函数在这种情况下不起作用.有人可以给些建议吗?预先感谢

I am pretty new to this Jquery extraction. My problem right now is to make the links I extracted automatically clicked through code in Chrome console. I used the code below to get the link in the href, but the click(),trigger('click') function doesn't work in this situation. Can someone give some suggestions? Thanks in advance

$('.agent-info').find('a').href.trigger('click')

推荐答案

您可以在NodeJ中使用requestcheerio模块来加载页面内容并获取所有正文.

You can use request and cheerio modules in NodeJs to load page contents and get all bodies.

let request = require('request'),
    cheerio = require('cheerio'),
    q = require('q')

您需要一个获取url并加载页面的功能

You need a function which get url and load the page

function loadPage(url) {
    var deferred = q.defer();

    request.get({
        url: url,
        timeout: 60000
    },
        function (error, response, body) {
            if (!error) {
                deferred.resolve(body);
            }
            else {
                deferred.reject(error);
            }
        }
    );

    return deferred.promise;
}

您需要在正文中找到所有链接.为此,使用这样的功能

You need to find all links in the body. For that use a function like this

function extractLinks(body) {
    var deferred = q.defer(),
        $ = cheerio.load(body),
        links = [];

    $(/*Your selector*/).each(function() {
        // add links to links array
    });

    deferred.resolve(links);
    return deferred.promise;
}

然后,您必须再次将每个项目的主体加载到links数组中.因此,再次使用loadPage模块.还有一个使用cheerio提取新页面数据的函数.

Then you must again load body of each item in links array. So use the loadPage module again. And a function using cheerio to extract your data of new pages.

loadPage(firstUrl)
    .then(function(result){
        extractLinks(result)
            .then(function(res){
                 var arr = [];
                 for(var r in res) {
                     arr.push(loadPage(res[r]));
                     q.all(arr)
                         .then()
                         .catch();
                 }
            });
    })
    .catch(function(error) {
        console.log(error);
    });

这篇关于提取多个链接并使用JQuery在控制台中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆