Skip to content

Instantly share code, notes, and snippets.

@Irio
Created January 5, 2020 12:01
Show Gist options
  • Select an option

  • Save Irio/b31a0be9ca7e1391118628d3dde3c975 to your computer and use it in GitHub Desktop.

Select an option

Save Irio/b31a0be9ca7e1391118628d3dde3c975 to your computer and use it in GitHub Desktop.
Brazilian municipalities and their Wikipedia links
// Run with:
// $ go get github.com/MontFerret/ferret
// $ ferret municipalities.fql > municipalities.json
LET baseURL = "https://pt.wikipedia.org"
LET startURL = CONCAT(baseURL, "/wiki/Lista_de_munic%C3%ADpios_do_Brasil_por_popula%C3%A7%C3%A3o")
LET municipalitySelector = ".wikitable tbody tr:not(:nth-child(1))"
LET doc = DOCUMENT(startURL)
FOR row in ELEMENTS(doc, municipalitySelector)
LET ibgeID = TRIM(ELEMENT(row, "td:nth-child(2)").innerText)
LET name = TRIM(ELEMENT(row, "td:nth-child(3)").innerText)
LET federativeUnit = TRIM(ELEMENT(row, "td:nth-child(4)").innerText)
LET population = TO_INT(TRIM(ELEMENT(row, "td:nth-child(5)").innerText))
LET municipalityURL = CONCAT(baseURL, ELEMENT(row, "td:nth-child(3) a").attributes.href)
LET doc = DOCUMENT(municipalityURL)
LET URLs = (
FOR URL IN ELEMENTS(doc, ".infobox_v2 tr:last-child td .external")
RETURN URL.attributes.href
)
RETURN {
ibge_id: ibgeID,
name,
federative_unit: federativeUnit,
urls: URLs,
wikipedia_url: municipalityURL,
population
}
This file has been truncated, but you can view the full file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment