Skip to content

Instantly share code, notes, and snippets.

@Ranner198
Last active March 4, 2019 19:41
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 Ranner198/80cf459b9d9a1ed543a97008879cf9e2 to your computer and use it in GitHub Desktop.
Save Ranner198/80cf459b9d9a1ed543a97008879cf9e2 to your computer and use it in GitHub Desktop.
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