Skip to content

Instantly share code, notes, and snippets.

@LeoMcA
Created May 26, 2019 22:53
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 LeoMcA/32970c997695f0077f919b7ebe0320bd to your computer and use it in GitHub Desktop.
Save LeoMcA/32970c997695f0077f919b7ebe0320bd to your computer and use it in GitHub Desktop.
Scrapes UK EU Election Data from BBC website, and adds up Leave and Remain (and Labour) votes
const fetch = require("node-fetch")
const cheerio = require("cheerio")
const url = "https://www.bbc.co.uk/news/topics/crjeqkdevwvt/the-uks-european-elections-2019"
var leave = [{ name: "Leave", value: 0 }]
var remain = [{ name: "Remain", value: 0 }]
var lexit = [{ name: "???", value: 0 }]
var other = [{ name: "Other", value: 0 }]
const calc = async html => {
const $ = cheerio.load(html)
$(".uk2019__party").each((i, el) => {
var name = $(el).find(".score__labels__grid-name").text().trim()
var percent = $(el).find(".score__grid__row--three .councils--total").text().replace("Total", "")
var number = Number(percent.replace("%",""))
switch (name) {
case "The Brexit Party":
case "Conservative":
case "UKIP":
leave[0].value += number
leave.push({ name: name, value: number })
break
case "Liberal Democrat":
case "Green":
case "Change UK":
case "Scottish National Party":
case "Plaid Cymru":
remain[0].value += number
remain.push({ name: name, value: number })
break
case "Labour":
lexit[0].value += number
lexit.push({ name: name, value: number })
break
default:
other[0].value += number
other.push({ name: name, value: number })
}
})
leave.forEach(x => {
console.log(`${x.name}: ${x.value}%`)
})
console.log("")
remain.forEach(x => {
console.log(`${x.name}: ${x.value}%`)
})
console.log("")
lexit.forEach(x => {
console.log(`${x.name}: ${x.value}%`)
})
}
const run = async () => {
try {
const res = await fetch(url)
const text = await res.text()
calc(text)
} catch (e) {
console.error(e)
}
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment