YouTube Search (youtube.com)
JavaScriptPublicby zx-apiAug 27, 2026, 12:13 AMExpires: Never158 views
1async function searchYutube(query) {
2 const url = 'https://www.youtube.com/youtubei/v1/search?prettyPrint=false';
3
4 const payload = {
5 context: {
6 client: {
7 clientName: 'WEB',
8 clientVersion: '2.20240514.01.00', // Versi client web
9 hl: 'en',
10 gl: 'US',
11 }
12 },
13 query: query
14 };
15
16 try {
17 const response = await fetch(url, {
18 method: 'POST',
19 headers: {
20 'Content-Type': 'application/json',
21 'X-YouTube-Client-Name': '1',
22 'X-YouTube-Client-Version': '2.20240514.01.00',
23 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
24 },
25 body: JSON.stringify(payload)
26 });
27
28 if (!response.ok) {
29 throw new Error(`HTTP Error! Status: ${response.status}`);
30 }
31
32 const data = await response.json();
33 const results = [];
34
35 const contents = data?.contents?.twoColumnSearchResultsRenderer?.primaryContents?.sectionListRenderer?.contents;
36
37 if (contents && Array.isArray(contents)) {
38 for (const section of contents) {
39 const items = section.itemSectionRenderer?.contents || section.richGridRenderer?.contents;
40
41 if (items && Array.isArray(items)) {
42 for (const item of items) {
43 const videoRenderer = item.videoRenderer || item.richItemRenderer?.content?.videoRenderer;
44
45 if (videoRenderer && videoRenderer.videoId) {
46 results.push({
47 id: videoRenderer.videoId,
48 title: videoRenderer.title?.runs?.map(r => r.text).join('') || 'No Title',
49 channel: videoRenderer.ownerText?.runs?.map(r => r.text).join('') || 'Unknown Channel',
50 views: videoRenderer.viewCountText?.simpleText || '0 views',
51 publishedTime: videoRenderer.publishedTimeText?.simpleText || '',
52 duration: videoRenderer.lengthText?.simpleText || 'LIVE',
53 thumbnail: videoRenderer.thumbnail?.thumbnails?.[0]?.url || ''
54 });
55 }
56 }
57 }
58 }
59 }
60
61 return results;
62 } catch (error) {
63 console.error('Gagal melakukan scraping YouTube:', error.message);
64 throw error;
65 }
66}
67
68(async () => {
69 try {
70 console.log('Mencari video...');
71 const hasil = await searchYutube('belajar nodejs dasar');
72 console.log(`\nDitemukan ${hasil.length} video. Menampilkan 3 teratas:`);
73 console.log(JSON.stringify(hasil.slice(0, 3), null, 2));
74 } catch (err) {
75 console.error('Terjadi kesalahan:', err);
76 }
77})();77 lines·2,676 chars·2.6 KB
Wwrap·Ffullscreen