Skip to content

Instantly share code, notes, and snippets.

@walkerdb
Last active August 29, 2015 14:25
Show Gist options
  • Save walkerdb/2992c13a9a8600003413 to your computer and use it in GitHub Desktop.
Save walkerdb/2992c13a9a8600003413 to your computer and use it in GitHub Desktop.
from urllib2 import urlopen, quote
# if you're running python 3, replace the above with the following:
# from urllib.request import urlopen
# from urllib.parse import quote
def retrieve_viaf_search_results(search_index, search_term, auth_source):
# url search template formatted to allow easy variable insertion
search_url_template = 'http://viaf.org/viaf/search/viaf?query=local.{0}+all+{1}+and+local.sources+any+{2}&sortKeys=holdingscount&httpAccept=application/xml'
# since we'll be inserting the three passed variables into the
# html search template, we need to make sure to "escape" any
# special characters. VIAF also requires that they be enclosed in
# quotes, which themselves have to be escaped. We can use urllib2's
# "quote" function for this:
auth_source = quote('"{}"'.format(auth_source))
search_term = quote('"{}"'.format(search_term))
# build the full search url
search_url = search_url_template.format(search_index, search_term, auth_source)
# query the url with urlopen and store the returned xml
response = urlopen(search_url).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment