forked from sautumn/AllSides-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsscrape.js
89 lines (67 loc) · 2.48 KB
/
newsscrape.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var request = require('request');
// var rp = require('request-promise'); //maybe later :)
var cheerio = require('cheerio');
var lodash = require('lodash');
var underscoreDeepExtend = require('underscore-deep-extend');
var fs = require('fs');
var file = fs.createWriteStream('biasRatings.txt');
var _ = require('lodash');
_.mixin({ deepExtend: underscoreDeepExtend(_) });
var data = {};
var options = {
method: 'GET',
url: 'http://www.allsides.com/bias/bias-ratings',
qs:
{
field_news_source_type_tid: '2',
field_news_bias_nid: '1',
field_featured_bias_rating_value: 'All',
title: ''
},
form: {}
};
const pages = ['']
for (var i = 1; i < 30; i++) {
pages.push(String(i))
}
pages.forEach(function (page) {
var optionsPages = _.deepExtend(options, { qs: { page: page } });
request(optionsPages, function (error, response, body) {
if (error) throw new Error(error);
var $ = cheerio.load(body);
var dataObj = { title: null, rating: null, url: null };
$('.source-title').each(function (i, element) {
``
var title = element.parent.children[0].next.children[0].next.children[0].data;
var rating = element.parent.children[2].next.children[0].next.children[0].attribs.title;
var rating = rating.split(': ');
var link = element.parent.children[0].next.children[0].next.children[0].parent.attribs.href;
//we must go deeper to get the urls!!
var pageLink = 'http://www.allsides.com/' + link;
var options = {
method: 'GET',
url: pageLink
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
var $ = cheerio.load(body);
$('.dynamic-grid').each(function (i, element) {
var url = element.children[1].attribs.href;
var domain = url.replace(/^https?:\/\//, ''); // Strip off https:// and/or http://
domain = domain.replace(/^www\./, ''); // Strip off www.
domain = domain.split('/')[0]; // Get the domain and just the domain (not the path)
data[domain] = { title: title, rating: rating[1], url: url };
});
//write to file
fs.writeFile('biasRatings.json', JSON.stringify(data), function (err) {
if (err) throw new Error(error);
if (page === pages[pages.length - 1]) {
console.log("Domains stored: ", Object.keys(data).length);
} else {
console.log('Writing to file...');
}
});
});
});
});
})