Skip to content

Instantly share code, notes, and snippets.

@joelgriffith
Created January 15, 2018 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelgriffith/d3fa4860cfb41dffe93390ebf325828f to your computer and use it in GitHub Desktop.
Save joelgriffith/d3fa4860cfb41dffe93390ebf325828f to your computer and use it in GitHub Desktop.
Get the title from CNN
const puppeteer = require('puppeteer');
function getTitle() {
if (document.querySelector('meta[property="og:title"]')) {
return document.querySelector('meta[property="og:title"]').content;
}
if (document.querySelector('[itemprop="name"]')) {
return document.querySelector('[itemprop="name"]').text;
}
if (document.querySelector('title')) {
return document.querySelector('title').text;
}
return window.location.href; // Print URL as a fallback
}
async function run() {
const browser = await puppeteer.connect({
browserWSEndpoint: 'wss://chrome.browserless.io'
});
const page = await browser.newPage();
await page.goto('https://cnn.com');
const title = await page.evaluate(getTitle);
console.log(title);
browser.close();
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment