17 lines
513 B
JavaScript
17 lines
513 B
JavaScript
const fetch = require('node-fetch');
|
|
|
|
async function webSearch(query) {
|
|
const url = `https://duckduckgo.com/html/?q=${encodeURIComponent(query)}`;
|
|
const response = await fetch(url);
|
|
const html = await response.text();
|
|
// Very naive parsing: extract titles from <a> tags
|
|
const titles = [];
|
|
const regex = /<a class="result__a"[^>]*>([^<]+)<\/a>/g;
|
|
let match;
|
|
while ((match = regex.exec(html)) !== null) {
|
|
titles.push(match[1]);
|
|
}
|
|
return titles.slice(0, 5);
|
|
}
|
|
|
|
module.exports = { webSearch }; |