-
Notifications
You must be signed in to change notification settings - Fork 7
/
weapscrap.js
75 lines (57 loc) · 1.95 KB
/
weapscrap.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
const puppeteer = require('puppeteer')
void (async () => {
try {
const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage()
await page.goto('http://sasweaponsid.roadmapmedia.ch/')
// grab weapon data
const weaps = await page.evaluate(() => {
// a helper function for some slight code reuse
// grab the TD, the text and remove trailing whitespace
// What if there is no class name...?
/*const grabFromRow = (row, index) => row
.querySelectorAll(`td${[index]}`)
.innerText
.trim()*/
const grabFromList = (node, index) => node
.children[index]
.innerText
.trim();
const SELECTEUR = ['tr.parent-weapon', 'tr.child-weapon']
// we'll store our data in an array of objects
const data = []
// get all team rows
const weaponList = document.querySelectorAll(SELECTEUR);
// const weaponList = document.querySelectorAll(['tr.parent-weapon', 'tr.child-weapon'])
console.log(weaponList);
console.log('Before loop!');
// loop over each team row, creating objects
for (const weapon of weaponList) {
data.push({
type : grabFromList(weapon, 0),
name : grabFromList(weapon, 1),
calibre : grabFromList(weapon, 2),
regions : grabFromList(weapon, 3)
})
/*data.push({
type : grabFromRow(wr, 0),
name: grabFromRow(wr, 1),
calibre: grabFromRow(wr, 2),
regions: grabFromRow(wr, 3)
})*/
}
// send the data back into the teams variable
return data
})
// sauvegarder en JSON
const fs = require('fs');
fs.writeFile(
'WeaponsDB.json',
JSON.stringify(weaps, null, 2),
(err) => err ? console.error('Data non écrite!', err) : console.log('Données inscrites!')
)
await browser.close()
} catch (error) {
console.log(error)
}
})()