Working with the API
In this module, you will learn to:
- read API documentation to understand the possible queries
- limit search results with different variables
Now that we've learned to make a simple request using the DPyLA library, let's spend some time exploring the different kinds of requests we can make.
Constructing API Requests
First, let's get some contextual data about the search results. In the Python Interactive Interpreter, run result.count. This tells you how many total items fit the search term.
Now run result.limit. This tells you how many results the API returned. By default, the API will give you 10 results at a time. Look at the DPyLA documentation and try to construct a request that gets you 50 results.
You can also limit your search results with a variety of variables.
For example, you can be more specific with your search, such as limiting your query to cooking in the title of an item.
result = dpla.search("cooking", fields=["sourceResource.title", "sourceResource.date", "sourceResource.spatial"])
To see your updated search results, run:
You can also sort by geography:
result = dpla.search("cooking", fields=["sourceResource.title", "sourceResource.date", "sourceResource.spatial"], spatial_sort=[37.7577,-122.4376])
To see the results, run:
These types of commands help you focus the search results so that you have less noisy data – so that more of the data fits the questions you want to ask.
Work with the documentation and your table to construct some more targeted queries.
- How would you narrow the data for different questions? What variables would you use?
- Do you see any additional strengths or weaknesses with the data when trying to limit your search results?
How would you save one of your limited searches to a different variable? How then would you show the items?