Public
Edited
Nov 28, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rawdata = getCached(`https://en.wikipedia.org/w/api.php?format=json&origin=*&action=parse&prop=text&page=Cheese_Shop_sketch&section=3`)
Insert cell
Insert cell
data = {
// Turn our html table into an array.
const parser = new DOMParser();
const external = parser.parseFromString(rawdata.text["*"], "text/html");
const table = external.querySelectorAll("table")[1].cloneNode(true)
const trs = table.querySelectorAll("tr")
const data = [...trs.values()].slice(1).map(tr => {
try {
const dArray = [...tr.querySelectorAll("td").values()].map(n => n.textContent)
console.log(dArray)
// Links dont know they're on a different page now.
let link;
const rawLink = tr.querySelector("a")?.href
if (rawLink) {
const url = rawLink ? new URL(rawLink) : null;
url.hostname = 'en.wikipedia.org'
link = url.href
}
return {cheese: dArray[0].trim(), version: dArray[1].trim(), link: link}
} catch (error) {
return {error}
}
})
return data
}

Insert cell
getCached = {
// Cache wikipedia requests so we dont make too many.
const cacheData = {}
var count = 0
const func = async (url) => {
if (url in cacheData) return cacheData[url];
const resp = await fetch(url)
const jsonData = await resp.json()
const d = await jsonData.parse
cacheData[url] = d
count += 1
return cacheData[url]
}
func.data = () => (cacheData)
func.getCount = () => (count)
return func
}
Insert cell
getCached.data()
Insert cell
// Check our cache is working
now && getCached.getCount()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more