Skip to content

Instantly share code, notes, and snippets.

@osbre
Last active June 23, 2019 22:09
Show Gist options
  • Save osbre/6ec48c9ec5554cd770cd55e7674ff364 to your computer and use it in GitHub Desktop.
Save osbre/6ec48c9ec5554cd770cd55e7674ff364 to your computer and use it in GitHub Desktop.
DuckDuckGo search module (with puppeteer)
var search = require('./search.js');
search('github').then(function (links) {
console.log(links)
});
const puppeteer = require('puppeteer');
module.exports = async function (text) {
const browser = await puppeteer.launch({executablePath: 'chromium'})
const page = await browser.newPage()
await page.goto('https://duckduckgo.com/', { waitUntil: 'networkidle2' })
await page.type('#search_form_input_homepage', text)
await page.$eval('#search_form_input_homepage', el => el.value)
await page.$eval('input[id=search_button_homepage]', el => el.click());
await page.waitForNavigation();
const hrefs = await page.$$eval('#links a', as => as.map(a => a.href));
await browser.close()
hrefs.pop();
return hrefs;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment