Last active
June 23, 2019 22:09
-
-
Save osbre/6ec48c9ec5554cd770cd55e7674ff364 to your computer and use it in GitHub Desktop.
DuckDuckGo search module (with puppeteer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var search = require('./search.js'); | |
search('github').then(function (links) { | |
console.log(links) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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