-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
90 lines (77 loc) · 2.35 KB
/
scraper.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
89
90
import axios from 'axios';
import puppeteer from 'puppeteer';
import * as cheerio from 'cheerio';
export class EnhancedScraper {
constructor() {
this.baseUrl = 'https://duckduckgo.com/ac/';
this.searchUrl = 'https://duckduckgo.com/';
}
async getSuggestions(query) {
try {
const response = await axios.get(this.baseUrl, {
params: {
q: query,
kl: 'fr-fr'
},
headers: {
'Accept': 'application/json'
}
});
return response.data.map(item => ({
phrase: item.phrase,
category: item.category || 'général',
score: Math.random() * 100 // Score de pertinence simulé
}));
} catch (error) {
console.error('Erreur suggestions:', error.message);
return [];
}
}
async getRelatedSearches(query) {
try {
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox']
});
const page = await browser.newPage();
await page.goto(`${this.searchUrl}?q=${encodeURIComponent(query)}`);
// Attendre le chargement des résultats
await page.waitForSelector('.results', { timeout: 5000 });
const content = await page.content();
const $ = cheerio.load(content);
const relatedSearches = [];
// Extraire les recherches connexes
$('.related-searches a').each((_, element) => {
relatedSearches.push($(element).text().trim());
});
await browser.close();
return relatedSearches;
} catch (error) {
console.error('Erreur recherches connexes:', error.message);
return [];
}
}
async getSearchVolume(query) {
// Simulation de volume de recherche
return Math.floor(Math.random() * 10000);
}
async getFullAnalysis(query) {
const [suggestions, relatedSearches] = await Promise.all([
this.getSuggestions(query),
this.getRelatedSearches(query)
]);
const searchVolume = await this.getSearchVolume(query);
return {
keyword: query,
suggestions,
relatedSearches,
searchVolume,
timestamp: new Date().toISOString(),
metrics: {
totalSuggestions: suggestions.length,
totalRelated: relatedSearches.length,
averageScore: suggestions.reduce((acc, curr) => acc + curr.score, 0) / suggestions.length || 0
}
};
}
}