scraperjavascript3811
Detik.com News Scraper (Search, Latest, Popular & Article Reader)
Scraper berita portal Detik.com terlengkap: Pencarian berita dengan pagination, berita terkini (indeks), berita terpopuler (trending), dan pembaca artikel berita lengkap.
Vamz Spectre16 Okt 2026290 baris kode
Jalankan via Terminal / CLI:
# 1. Download file script
curl -sL "https://code.vamxapi.my.id/snippet/raw/detik-scraper" -o detik-scraper.js
# 2. Jalankan script
node detik-scraper.js
Dependencies npm yang Dibutuhkan:
npm install cheerioPanduan Penggunaan:
node detik-scraper.js "<keyword_or_url>" [--latest] [--popular]
detik-scraper.js(290 lines)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Detik.com Scraper (Search, Latest, Popular & Article Reader)
* Author: Vamz Spectre / VamxAPI
*
* Scraper berita terlengkap detik.com:
* - Search Berita (Pencarian kata kunci dengan pagination)
* - Berita Terkini (Indeks berita terbaru)
* - Berita Terpopuler (Trending / paling banyak dibaca)
* - Baca Detail Berita (Judul, Penulis, Tanggal, Gambar Utama, Isi Lengkap Berita)
*
* Install / Download:
* curl -sL "https://code.vamxapi.my.id/snippet/raw/detik-scraper" -o detik-scraper.js
*
* Usage:
* - Search Berita : node detik-scraper.js "prabowo"
* - Berita Terkini: node detik-scraper.js --latest
* - Berita Populer: node detik-scraper.js --popular
* - Detail Berita : node detik-scraper.js "https://news.detik.com/berita/d-8709865/..."
*/
const cheerio = require('cheerio');
const HEADERS = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept-Language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'
};
/**
* Mencari berita berdasarkan kata kunci
* @param {string} query - Kata kunci pencarian
* @param {number} [page=1] - Halaman pencarian
* @returns {Promise<object>}
*/
async function search(query, page = 1) {
if (!query) throw new Error('Parameter query pencarian tidak boleh kosong.');
const url = `https://www.detik.com/search/searchall?query=${encodeURIComponent(query)}&page=${page}`;
const res = await fetch(url, { headers: HEADERS });
if (!res.ok) throw new Error(`Gagal mengambil data pencarian (${res.status} ${res.statusText})`);
const html = await res.text();
const $ = cheerio.load(html);
const articles = [];
$('.list-content__item').each((_, el) => {
const linkEl = $(el).find('a[dtr-ttl], .media__link, a').first();
const title =
linkEl.attr('dtr-ttl') ||
$(el).find('h3 a, h2 a, .media__title a').first().text().replace(/\s+/g, ' ').trim() ||
$(el).find('img').attr('alt') ||
'';
const articleUrl = $(el).find('a[dtr-ttl], .media__link, a').last().attr('href');
const image = $(el).find('img').attr('src') || null;
const category = $(el).find('.media__subtitle, h2').first().text().trim() || 'detikcom';
const date = $(el).find('.media__date span').attr('title') || $(el).find('.media__date').text().trim() || null;
const desc = $(el).find('.media__desc').text().replace(/\s+/g, ' ').trim() || null;
if (title && articleUrl && articleUrl.startsWith('http')) {
articles.push({
title,
category,
date,
description: desc,
image,
url: articleUrl
});
}
});
return {
status: true,
total: articles.length,
page: Number(page),
query,
results: articles
};
}
/**
* Mengambil indeks berita terkini detik.com
* @param {number} [page=1] - Halaman indeks
* @returns {Promise<object>}
*/
async function latest(page = 1) {
const url = `https://news.detik.com/indeks?page=${page}`;
const res = await fetch(url, { headers: HEADERS });
if (!res.ok) throw new Error(`Gagal mengambil berita terkini (${res.status} ${res.statusText})`);
const html = await res.text();
const $ = cheerio.load(html);
const articles = [];
const seenUrls = new Set();
$('.list-content__item, .media').each((_, el) => {
const linkEl = $(el).find('a[dtr-ttl], .media__link, a').first();
const title =
linkEl.attr('dtr-ttl') ||
$(el).find('h3 a, h2 a, .media__title a').first().text().replace(/\s+/g, ' ').trim() ||
$(el).find('img').attr('alt') ||
'';
const articleUrl = $(el).find('a[dtr-ttl], .media__link, a').last().attr('href');
const image = $(el).find('img').attr('src') || null;
const date = $(el).find('.media__date span').attr('title') || $(el).find('.media__date').text().trim() || null;
if (title && articleUrl && articleUrl.startsWith('http') && !seenUrls.has(articleUrl)) {
seenUrls.add(articleUrl);
articles.push({
title,
date,
image,
url: articleUrl
});
}
});
return {
status: true,
total: articles.length,
page: Number(page),
results: articles
};
}
/**
* Mengambil berita terpopuler detik.com
* @returns {Promise<object>}
*/
async function popular() {
const url = 'https://www.detik.com/terpopuler';
const res = await fetch(url, { headers: HEADERS });
if (!res.ok) throw new Error(`Gagal mengambil berita populer (${res.status} ${res.statusText})`);
const html = await res.text();
const $ = cheerio.load(html);
const articles = [];
const seenUrls = new Set();
$('.list-content__item, article.list-content__item').each((_, el) => {
const linkEl = $(el).find('a[dtr-ttl], .media__link, a').first();
const title =
linkEl.attr('dtr-ttl') ||
$(el).find('h3 a, h2 a, .media__title a').first().text().replace(/\s+/g, ' ').trim() ||
$(el).find('img').attr('alt') ||
'';
const articleUrl = $(el).find('a[dtr-ttl], .media__link, a').last().attr('href');
const image = $(el).find('img').attr('src') || null;
const category = $(el).find('.media__subtitle, h2').first().text().trim() || 'detikcom';
const date = $(el).find('.media__date span').attr('title') || $(el).find('.media__date').text().trim() || null;
if (title && articleUrl && articleUrl.startsWith('http') && !seenUrls.has(articleUrl)) {
seenUrls.add(articleUrl);
articles.push({
title,
category,
date,
image,
url: articleUrl
});
}
});
return {
status: true,
total: articles.length,
results: articles
};
}
/**
* Membaca detail isi artikel berita lengkap
* @param {string} articleUrl - URL artikel berita detik.com
* @returns {Promise<object>}
*/
async function detail(articleUrl) {
if (!articleUrl || !articleUrl.startsWith('http')) {
throw new Error('Berikan URL artikel berita detik.com yang valid.');
}
// Menambahkan ?single=1 agar seluruh halaman berita tampil lengkap dalam 1 request
const fetchUrl = articleUrl.includes('?') ? `${articleUrl}&single=1` : `${articleUrl}?single=1`;
const res = await fetch(fetchUrl, { headers: HEADERS });
if (!res.ok) throw new Error(`Gagal membuka artikel berita (${res.status} ${res.statusText})`);
const html = await res.text();
const $ = cheerio.load(html);
const title = $('h1.detail__title, h1.title, .detail__title').first().text().replace(/\s+/g, ' ').trim();
const author = $('.detail__author, .author').first().text().replace(/\s+/g, ' ').trim() || null;
const date = $('.detail__date, .date').first().text().replace(/\s+/g, ' ').trim() || null;
const image = $('.detail__media-image img, .detail__media img, figure img').first().attr('src') || null;
const imageCaption = $('.detail__media-caption, figcaption').first().text().replace(/\s+/g, ' ').trim() || null;
// Bersihkan konten dari iklan, tag sisipan, dan script
const bodyEl = $('.itp_bodycontent, .detail__body-text, .read__content');
bodyEl.find('script, style, .detail__body-tag, .sisip_video_ds, .paradetiks, .linksisip, div[id^="crosslink"], .banner').remove();
const paragraphs = [];
bodyEl.find('p').each((_, p) => {
const text = $(p).text().replace(/\s+/g, ' ').trim();
if (
text &&
!text.toLowerCase().includes('simak juga') &&
!text.toLowerCase().includes('baca juga') &&
!text.toUpperCase().includes('SCROLL TO CONTINUE')
) {
paragraphs.push(text);
}
});
const fullContent = paragraphs.length > 0 ? paragraphs.join('\n\n') : bodyEl.text().replace(/\s+/g, ' ').trim();
return {
status: true,
title,
author,
date,
image,
imageCaption,
content: fullContent,
url: articleUrl
};
}
module.exports = {
search,
latest,
popular,
detail
};
// CLI Runner
if (require.main === module) {
const args = process.argv.slice(2);
const input = args[0];
const page = args[1] || 1;
if (!input) {
console.log(
JSON.stringify(
{
status: false,
error: 'Parameter diperlukan.',
usage: {
search: 'node detik-scraper.js "<keyword>" [page]',
latest: 'node detik-scraper.js --latest [page]',
popular: 'node detik-scraper.js --popular',
detail: 'node detik-scraper.js "<url_artikel>"'
}
},
null,
2
)
);
process.exit(1);
}
(async () => {
try {
let result;
if (input === '--latest' || input === '-l') {
result = await latest(page);
} else if (input === '--popular' || input === '-p') {
result = await popular();
} else if (input.startsWith('http://') || input.startsWith('https://')) {
result = await detail(input);
} else {
result = await search(input, page);
}
console.log(JSON.stringify(result, null, 2));
} catch (err) {
console.error(
JSON.stringify(
{
status: false,
error: err.message
},
null,
2
)
);
process.exit(1);
}
})();
}