We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
写爬虫的时候会用上 代码都是异步执行,也是个麻烦事。 当for循环中执行函数,是异步的,就会出现问题,上次用了一个很蹩脚的办法,把循环改成定时器
$.each(function (i, item) { self.core(item, backfun); //异步函数 });
改成了这样
var count = 0; var timeout1 = setInterval(function () { if (count == arr.length) { clearInterval(timeout1); return; } var index = count % arr.length; self.core(arr[index], backfun); count++; }, 1000);
这样还是不好,下面用async来控制流程.... 使用到了async.eachSeries,注意它的第二个函数,不光是用来处理错误的,也是回调的,有了它就可以连起来了
async.eachSeries(arr, function (item, callback) { console.log(item) self.core(item, function ($) { var $itemlist = $('.post-listing .item-list'); var linkList = $itemlist.map(function (idx, element) { var $element = $(element).find('.post-title a'); return $element.attr('href'); }).get(); console.log(linkList) async.eachSeries(linkList, function (item, callback2) { self.core(item, function ($) { var author = $('.post-inner .entry').find('p').eq(0).text(); var content = $.html('.article_text'); var text = $('.post-inner span[itemprop="name"]').text(); console.log("text=" + text); if ( !! text) { var thor = new articlema({ title: text, author: author, category: '002', content: content }); thor.save(function (err, thor) { if (err) return console.log(err); //console.log(thor); }); } sleep(500) callback2(null) }); }, function (err) { console.log("content==============" + err); sleep(500) callback(null) }); }) }, function (err) { console.log("item==============" + err); })

#2016-02-04
The text was updated successfully, but these errors were encountered:
No branches or pull requests
写爬虫的时候会用上
代码都是异步执行,也是个麻烦事。
当for循环中执行函数,是异步的,就会出现问题,上次用了一个很蹩脚的办法,把循环改成定时器
改成了这样
这样还是不好,下面用async来控制流程....
使用到了async.eachSeries,注意它的第二个函数,不光是用来处理错误的,也是回调的,有了它就可以连起来了

#2016-02-04
The text was updated successfully, but these errors were encountered: