Skip to content

Instantly share code, notes, and snippets.

@walkerdb
Last active August 29, 2015 14:25
Show Gist options
  • Save walkerdb/2e0bc7a448c3c3156640 to your computer and use it in GitHub Desktop.
Save walkerdb/2e0bc7a448c3c3156640 to your computer and use it in GitHub Desktop.
from lxml import etree
def get_lc_auth_from_viaf_data(response):
lc_auth = ""
# parse the returned xml into an lxml etree
tree = etree.fromstring(response)
# extract a list of the VIAF search result nodes using an xpath query
results = tree.xpath("//*[local-name()='record']")
# if there are any search results, grab the first one
if len(results) > 0:
primary_result = results[0]
# Extract all auth sources for this search result
sources = primary_result.xpath("//*[local-name()='mainHeadingEl']/*[local-name()='id']")
# There are usually a number of sources, but we only want the LoC
for source in sources:
if "LC|" in source.text:
# needs some formatting to extract the number only
lc_auth = source.text.split("|")[1].replace(" ", "")
break
return lc_auth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment