Skip to content

Instantly share code, notes, and snippets.

@rob-gordon
Created August 26, 2021 15:31
Show Gist options
  • Save rob-gordon/9cb58e631a34f8701648f8a50748f51e to your computer and use it in GitHub Desktop.
Save rob-gordon/9cb58e631a34f8701648f8a50748f51e to your computer and use it in GitHub Desktop.
get idiom
#!/usr/bin/env node
const request = require("request");
const cheerio = require("cheerio");
const args = process.argv.slice(2);
const search = args[0];
if (!search) throw new Error("Must pass search term");
const url = `https://idioms.thefreedictionary.com/${search.replace(
/ /gi,
"+"
)}`;
request(url, function (_error, _response, page) {
if (!page) throw new Error("Error Loading Page");
const $ = cheerio.load(page);
const ul = $("ul.idiKw");
if (ul.length) {
$(ul)
.find("li a")
.each((i, el) => {
console.log($(el).text());
});
} else {
console.log("No Results List");
}
});
{
"name": "gidiom",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"bin": "./index.js",
"dependencies": {
"cheerio": "^1.0.0-rc.10",
"request": "^2.88.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment