Thursday, 20 August 2020

Having difficulty scraping a subreddit for post title + link using cheerio and axios

Hi allI'm super new to node.js (literally programmed on it for the first time yesterday). I have some experience in other languages (Python and C#) but I've never delved into web scraping or anything advanced really.I'm trying to make a bot for me and my friends' Discord channel and I'm trying to build a web scraper that pulls the top 5 posts from /r/GameDealsI've followed a guide that I got to work to pull images from /r/dankmemes and post the results when the bot reads the word 'postmeme' in the discord and it works great!Here's what I have so far:const axios = require("axios"); const cheerio = require("cheerio"); const mainUrl = `https://reddit.com/r/dankmemes`; const randNo = (limit) => { const thatNo = Math.floor(Math.random() * limit); return thatNo; }; const dealWithData = (html) => { const $ = cheerio.load(html); const urlMeme = $("._2_tDEnGMLxpM6uOa2kaDB3.ImageBox-image.media-element._1XWObl-3b9tPy64oaG6fax"); const indexValue = randNo(urlMeme.length); bot.on('message', msg=>{ if(msg.content === "postmeme"){ msg.reply(`\n${urlMeme[indexValue].attribs.src}`); } }) }; TL;DR I'm way out of my depth I feel and would just like some guidance on how I might edit this code to pull a few links from /r/GameDeals instead.Also, I've been trying to figure out how I could get the link to change each time the trigger word (postmeme) is written, because as the code stands right now it seems to only populate a new link when I take the bot down and restart it.Any assistance would be very much appreciated!

Submitted August 20, 2020 at 08:16PM by Koenigspiel

No comments:

Post a Comment