ESRI Scraping

ESRI is sunsetting the Classic StoryMaps. Apparently that was announced in 2019 so they’ve been pretty generous. They provided a link to the content that is going to be retired. I couldn’t figure a decent way to export that content and copy/pasting was a mess.1

It is HTML though and that means I can take some javascript and run it in the console to grab that information. This gets a bit trickier because of shadowRoots.2

const items = document.querySelectorAll('arcgis-item-browser-table-row');

const data = Array.from(items).map(item => {
  const shadow = item.shadowRoot;
  const title = shadow.querySelector('.checkbox__title .cell--clamp')?.textContent.trim();
  const link = shadow.querySelector('.cell--link__container a')?.href;
  
  const userPopup = shadow.querySelector('arcgis-user-popup');
  const userAvatar = userPopup?.shadowRoot?.querySelector('arcgis-user-avatar');
  const owner = userAvatar?.shadowRoot?.querySelector('.title')?.textContent.trim();
  
  return { title, link, owner };
});

copy(JSON.stringify(data, null, 2));//copies the JSON to my clipboard

I have to do it for each page and do some minor combining, but once I have the content, I can do with it what I will. In this case, I took the combined JSON and then ran it back out to make the HTML I wanted to paste into an email.

const destination = document.querySelector('#list')

data.forEach(item => {
  const title = item.title
  const link = item.link
  const owner = item.owner
  const html = `<li><a href='${link}'>${title}</a> - ${owner}</li>`;
  destination.insertAdjacentHTML('beforeend', html)
})

The HTML is very exciting.

<ol id="list"></ol>

1 That could be on me. I find the ESRI online stuff to be just a mess.

2 Doesn’t shadowroot sound so cool?

2 thoughts on “ESRI Scraping”

  1. Hi Tom, sorry I don’t have access to that table, but you should be able to use ArcGIS REST JS, the Python API or the REST API directly to download the items. I would try with SearchQueryBuilder (https://developers.arcgis.com/arcgis-rest-js/api-reference/arcgis-rest-portal/SearchQueryBuilder/) to find the items (you will likely need to use the typekeywords filter: https://developers.arcgis.com/rest/users-groups-and-items/search-reference/) and as soon as you have the item IDs, you will need the items data (you can retrieve it with https://developers.arcgis.com/arcgis-rest-js/api-reference/arcgis-rest-portal/getItemData/).

    I hope this helps!

Leave a Reply