Created
November 16, 2015 15:38
-
-
Save prozbik/398f97b33fb1565ffa7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Nightmare = require('nightmare'); | |
var fs = require('fs'); | |
var getContent = function(i, data) { | |
var i = i || 0; | |
if(i < data.length) { | |
var n = new Nightmare() | |
.useragent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36") | |
.goto(data[i]) | |
.wait() | |
.evaluate(function() { | |
return { | |
title: $('.title').text(), | |
text: $('.text p').text() | |
} | |
}).run(function (err,result) { | |
var fileData = '\n' + result.title + '\n' + 'Українська правда' + '\n' + result.text + '\n'; | |
fs.appendFile('./result.doc', fileData , function (err) { | |
if(err) throw err; | |
console.log('Додано ' + i + ' з ' + data.length); | |
n.proc.disconnect(); | |
n.proc.kill(); | |
n.ended = true; | |
i++; | |
getContent(i,data); | |
}) | |
}) | |
} | |
} | |
var getUrl = function(site) { | |
var n = new Nightmare() | |
.useragent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36") | |
.goto(site) | |
.wait() | |
.evaluate(function() { | |
var arr = []; | |
$('.news4 dd.flash > a').each(function (i) { | |
arr.push($(this).prop('href')) | |
}); | |
return arr; | |
}).run(function (err,result) { | |
if(err) console.error(err); | |
console.log("Знайдено " + result.length + " статті"); | |
getContent(0,result); | |
}).end(function() {}) | |
} | |
getUrl('http://www.pravda.com.ua/news'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment