-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper.js
More file actions
104 lines (88 loc) · 3.02 KB
/
Copy pathscraper.js
File metadata and controls
104 lines (88 loc) · 3.02 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const puppeteer = require("puppeteer");
const fs = require("fs/promises");
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
async function geraJson(url, quantidade) {
console.log('scraping')
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const retorno = await selecionaImagensPaginas(page, quantidade)
await delay(4000);
browser.close()
await fs.writeFile("public/lista.json", JSON.stringify(retorno))
const link = "<a href='lista.json'>download</a>"
return link
};
async function selecionaImagensPaginas(page, quantidade) {
const retorno = []
var json_aux = {}
json_aux["Racas Sem Links"]= await nomeSemLink(page)
retorno.push(json_aux)
const links = await listaLinks(page)
const nomes = await listaNomes(page)
//acessa todos os links e seleciona as imagens de cada página
for (let m = 0; m < quantidade; m++) {
const json_aux = {}
await page.goto(links[m]);
page.waitForNavigation()
const imagens = await avaliaPagina(page)
console.log(nomes[m])
json_aux[nomes[m]] = imagens;
retorno.push(json_aux)
if(await verificaPag(page)){
console.log("Pag no padrao")
}
}
return JSON.parse(JSON.stringify(retorno))
}
async function listaLinks(page) {
return page.evaluate(() => {
return Array.from(document.querySelectorAll(".div-col ul li > a")).map(n => n.href)
})
}
async function listaNomes(page) {
return page.evaluate(() => {
return list = Array.from(document.querySelectorAll(".div-col ul li > a")).map(n => n.title)
})
}
async function verificaPag(page) {
return page.evaluate(() => {
list = Array.from(document.querySelectorAll("#mw-content-text > div.mw-parser-output > table.infobox.biota")).map(n => n.outerHTML)
if(list.length != 0){
return true
}
return false
})
}
async function nomeSemLink(page){
return page.evaluate(() => {
const lista = Array.from(document.querySelectorAll(".div-col ul li")).map(n => n.firstChild.data)
const newList = Array.from(document.querySelectorAll(".div-col ul li > span")).map(n => n.id)
for (item of lista){
if (item != null){
newList.push(item)
}
}
return newList
})
}
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
async function avaliaPagina(page) {
return page.evaluate(() => {
const lista = [];
//Percorre todas as imagens da página e as filtra pelo tamanho
for (img of document.querySelectorAll("img")) {
if (img.height > 70 && img.width > 70) {
lista.push(img.src);
}
}
return lista
});
}
module.exports = {
geraJson,
}