Created
January 15, 2018 23:35
-
-
Save joelgriffith/d3fa4860cfb41dffe93390ebf325828f to your computer and use it in GitHub Desktop.
Get the title from CNN
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'); | |
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