Last active
March 4, 2019 19:41
-
-
Save Ranner198/80cf459b9d9a1ed543a97008879cf9e2 to your computer and use it in GitHub Desktop.
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 cheerio = require('cheerio'); | |
const request = require('request'); | |
var URL = 'https://www.reddit.com/r/dankmemes/'; | |
request(URL, function(err, resp, html) { | |
//If there is no error | |
if (!err){ | |
//The URL Data | |
const $ = cheerio.load(html); | |
//Save embeded urls | |
var returnInfo = []; | |
//Treverse the webpage and select the media elements | |
$('.media-element').each(function(i, element){ | |
var temp = $(this).attr('src'); //Create a reference for the image | |
returnInfo.push(temp); //Add the URL address to the return info array | |
}); | |
//Generate a random number from the length of the returned image urls | |
var randomNum = Math.floor(Math.random() * returnInfo.length); | |
//Try to output the url, if it doesn't exist or there is a problem it will log it out for us | |
try { | |
//Output the url for the image you can embed this somewhere and it will show it | |
console.log(returnInfo[randomNum]); | |
} catch(e) { | |
//Output the error | |
console.log("Error in the output process: " + e); | |
} | |
} else { | |
//There was an error with our request | |
console.log("Error in webscrape process: " + err); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment