Parsing the HTML and showing the result

This is all we'll need to scrape the HTML and get the status. Inside scraper-alert-j5.js, we're going to add cheerio's require() call to the top of the file with the other requires: 

const cheerio = require('cheerio')

Then, we're going to modify the callback that fires when request is done fetching the HTML. We're going to add the cheerio call to load the text and look for the first p child of the div with ID domain-main-content, and pull out its text. Then, we'll see if that text contains It's just you. and write to the LCD:

function isJohnnyFiveDown() {
request('https://downforeveryoneorjustme.com/johnny-five.io',
(err, resp, body) => {
let $ = cheerio.load(body)
let statusText = $('#domain-main-content p')[0].text()
LCD.clear()
LCD.home()
LCD.print('johnny-five.io')
LCD.cursor(0, 1)
// make sure to use " to surround the string!
if(statusText.contains("It's just you.")){
LCD.print('is up!')
} else {
LCD.print('is down (possibly)!')
}
})
}

We're ready to load it and run it! Load your project onto your Pi, navigate to the folder in your Pi SSH session, and run the following commands:

npm i
sudo node scraper-alert-j5.js

And you should see whether Johnny-Five is up or not on your LCD!

You may have noticed I put possibly in the down condition. This is because, as I mentioned before, HTML scraping is very brittle.  If they change It's just you. to It is just you., our code will break! So I like to remind the LCD viewer that it may not necessarily be down. Again, this is an example of why, if you can find it, it's better to get data from an API. But sometimes there's no real choice.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.157.45