Created
April 10, 2020 11:19
-
-
Save pczajkowski/dac5d384c0478b3258e5281b7b4e8707 to your computer and use it in GitHub Desktop.
printTextForGivenElement.js
This file contains hidden or 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 fs = require('fs'); | |
const cheerio = require('cheerio'); | |
const puppeteer = require('puppeteer'); | |
let app = {}; | |
app.parse = async function(data, elementSelector) { | |
if (data.length === 0) { | |
console.log("No data!"); | |
return; | |
} | |
const $ = cheerio.load(data); | |
const elements = $(elementSelector); | |
elements.each(function(_, elem) { | |
const text = $(this).text(); | |
if (text.length > 0) { | |
console.log(text); | |
} else { | |
console.log($.html(this)); | |
} | |
}); | |
}; | |
app.getDataFromURL = async function getPage(link) { | |
const device = puppeteer.devices['iPad Pro landscape']; | |
const browser = await puppeteer.launch({args: ['--no-sandbox']}); | |
const page = await browser.newPage(); | |
await page.emulate(device); | |
await page.goto(link, {waitUntil: 'networkidle2'}); | |
const content = await page.content(); | |
await browser.close(); | |
return content; | |
}; | |
app.getDataFromFile = function(file, elementSelector) { | |
fs.readFile(file, "utf8", function read(err, data) { | |
if (err) { | |
throw err; | |
} | |
app.parse(data, elementSelector); | |
}); | |
}; | |
app.usage = function() { | |
console.log(`You must specify path to the file and element selector! | |
Possible selectors: | |
<element_name> | |
.<class_name> | |
#<id_name>`); | |
}; | |
app.main = async function() { | |
if (process.argv.length < 4) { | |
app.usage(); | |
return; | |
} | |
const path = process.argv[2]; | |
const elementSelector = process.argv[3]; | |
if (fs.existsSync(path)) { | |
app.getDataFromFile(path, elementSelector); | |
} else { | |
const data = await app.getDataFromURL(path); | |
app.parse(data, elementSelector); | |
} | |
}; | |
app.main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment