Download Code Snippets

Sample code for downloading materials, including sample scripts.

Downloading Library of Congress Records by Control No

This uses curls, potentially with sleeping between to avoid getting shut out. Sleep of 1 second on MARCXML and Sleep of 3 on the id service. These are not guarantees you won’t get locked out.

Download MARCXML

curl https://lccn.loc.gov/sh2018002914/marcxml -o sh2018002914.xml

Or working from a simple (and short) list:

for lccn in nb2015017302 sh2018002914 n89223874; do curl https://lccn.loc.gov/$lccn/marcxml -o $lccn.xml; sleep 1; done

note that in the above, you can mix names, subjects, etc. Separate the lccns in this download with a single space. Added a second of sleep to avoid getting shut out. If it gets longer, consider making a full bash script, as below:

#!/bin/bash
for lccn in nb2015017302 sh2018002914 n89223874 n82203435 sh85033801
do
  curl https://lccn.loc.gov/$lccn/marcxml -o $lccn.xml
  sleep 1
done

save as marc_download.sh and run chmod u+x marc_download.sh. Run ./marc_download.sh to execute.

Download MADSRDF as JSON by LCCN

This should be easy enough to adapt to similar kinds of downloads you might do. Simply example the URL you would use to download a single file (e.g. is it actually nb2015017302.json or nb2015017302.madsrdf.nt or nb2015017302.skos.json, etc.). Note: there are other ways to download these files. This is just one simple one!

curl http://id.loc.gov/authorities/names/nb2015017302.madsrdf.json -o nb2015017302.json

An important difference here is you have to know what kind of authority you’re querying. So you’ll need to do names, subjects, etc. separately, whereas for the lccn query, it’s straight-up lccn.

Working from a simple list of names:

for lccn in nb2015017302 n89223874; do curl http://id.loc.gov/authorities/names/$lccn.madsrdf.json -o $lccn.json; sleep 3; done

or subjects:

for lccn in sh2016002835 sh86003754; do curl http://id.loc.gov/authorities/subjects/$lccn.madsrdf.json -o $lccn.json; sleep 3; done

Separate lccns with a single space. Again. Be sure to check whether you’re doing name, subject, etc.

  • name = /names/
  • subject = /subjects/
  • other things will have their own too…

If it gets longer, consider making a full bash script, as below:

#!/bin/bash
for lccn in nb2015017302 n89223874 n82203435 n2017074015
do
  curl http://id.loc.gov/authorities/names/$lccn.madsrdf.json -o $lccn.json
  sleep 3
done

See instructions on downloading MARCXML above for info on saving your script and making it executable.