Skip to content

Instantly share code, notes, and snippets.

@tdjsnelling
Created July 19, 2018 11:29
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 tdjsnelling/bad3b7a10e31a491183a159ee50cd9c4 to your computer and use it in GitHub Desktop.
Save tdjsnelling/bad3b7a10e31a491183a159ee50cd9c4 to your computer and use it in GitHub Desktop.
scrape games from hq buff
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
try {
var hq = require('./hq.json');
}
catch (e) {
var hq = [];
}
if (!process.argv[2]) {
throw 'Date must be supplied in format YYYY-MM-DD or YYYY-MM-DD/number';
}
var id = process.argv[2];
var url = 'http://hqbuff.com/game/' + id;
var game = {
id: id,
url: url,
prize: null,
questions: []
}
request(url, (err, res, body) => {
if (err) {
throw err;
}
var $ = cheerio.load(body);
game.prize = $('.game__prize').find('span').text();
$('.question').each((i, el) => {
var question = {};
question.question = $(el).find('.question__text').text().replace(/[\t,\n]/g,'').replace('Savage', '');
question.answers = {};
question.answers.correct = $(el).find('.questions__correct').text().replace(/[\t,\n]/g,'').replace(' Correct', '');
question.answers.other = [];
$(el).find('li').each((i, el) => {
if ($(el).children().length == 0) {
question.answers.other.push($(el).text());
}
});
game.questions.push(question);
});
hq.push(game);
fs.writeFile('hq.json', JSON.stringify(hq, null, 4), (err) => {
if (err) throw err;
});
});
{
"name": "hq-scraper",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^1.0.0-rc.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment