提取多个链接并使用JQuery在控制台中单击

我对这个Jquery提取很新。 我现在的问题是让我提取的链接自动点击Chrome控制台中的代码。 我使用下面的代码来获取href中的链接,但click(),trigger('click')函数在这种情况下不起作用。 有人能提出一些建议吗? 提前致谢

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

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

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

您需要一个获取url并加载页面的函数

 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; } 

您需要找到正文中的所有链接。 为此使用这样的function

 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来提取新页面数据的function。

 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); }); 

使用$(".agent-info a").trigger("click"); 代替。 这是一个简单的例子:

 $(document).on("click", ".agent-info a", function(){ $(this).text("It works!"); }); $(document).ready(function(){ $(".agent-info a").trigger("click"); });