-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (64 loc) · 2.68 KB
/
index.js
File metadata and controls
79 lines (64 loc) · 2.68 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
const express = require("express");
const puppeteer = require("puppeteer");
const app = express();
// Rota raiz
app.get("/", (req, res) => {
res.send("Bem vindo ao Scraper Google Maps");
});
// Rota de busca no Google Maps
app.get("/search", async (req, res) => {
const searchTerm = req.query.term;
if (!searchTerm) {
return res.status(400).json({ error: "O parâmetro 'term' é obrigatório." });
}
try {
const browser = await puppeteer.launch({
headless: true,
executablePath: "/usr/bin/google-chrome", // Caminho para o Chrome instalado
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--lang=pt-BR", // Define o idioma do navegador como português
],
});
const page = await browser.newPage();
// Configura o cabeçalho de idioma
await page.setExtraHTTPHeaders({
"Accept-Language": "pt-BR,pt;q=0.9",
});
// Gera a URL de pesquisa do Google Maps
const url = `https://www.google.com/maps/search/${encodeURIComponent(searchTerm)}`;
await page.goto(url, { waitUntil: "networkidle2" });
console.log(`Pesquisando: ${searchTerm}`);
// Seletor para os resultados
const resultsSelector = `[aria-label="Resultados para ${searchTerm}"]`;
await page.waitForSelector(resultsSelector, { timeout: 60000 }); // Aumenta o tempo limite para o carregamento
// Rolar a página até carregar todos os resultados
let previousHeight;
while (true) {
const resultDiv = await page.$(resultsSelector);
previousHeight = await page.evaluate((el) => el.scrollHeight, resultDiv);
await page.evaluate((el) => el.scrollBy(0, el.scrollHeight), resultDiv);
await new Promise((resolve) => setTimeout(resolve, 6000)); // Aguarda 6 segundos entre as rolagens
const newHeight = await page.evaluate((el) => el.scrollHeight, resultDiv);
if (newHeight === previousHeight) break; // Sai do loop se não houver mais resultados
}
// Extrair os websites dos resultados
const websites = await page.evaluate(() => {
const elements = document.querySelectorAll('[data-value="Website"]');
return Array.from(elements).map((el) => el.getAttribute("href"));
});
await browser.close();
// Retorna os resultados como JSON
return res.json({
term: searchTerm,
websites,
});
} catch (error) {
console.error("Erro ao realizar a pesquisa:", error);
return res.status(500).json({ error: "Erro ao realizar a pesquisa." });
}
});
// Inicializar o servidor
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Servidor rodando na porta ${PORT}`));