The MET API#
This session examines how to use the MET API in practice to gather and explore data.
Through the session, we will:
learn the components of an API request: the root, path, and endpoint
practice with a few handy functions for examining request data
type()anddir()
learn about a super important data type, the
dictionaryordict, and how to access items within that structure.create more requests with that data
f-strings
sort through request data using loops and conditionals
transform our data into a dataframe, for doing simple data analysis
import requests
# the structure of our request: base_url, path, query
base_url = "https://collectionapi.metmuseum.org"
path = "/public/collection/v1/search"
query = "?q=woman"
the anatomy of an API request:#
the root which consists of the base URL.
the path which consists of a directory structure (file structure) where the data is held.
/public/collection/v1/objects
/public/collection/v1/departments
/public/collection/v1/search
the parameter or the endpoint which is the specific request. For example, a query parameter takes a search term following
?q=, like:?q=woman
?q=van+gogh
A complete sample request that searches for the “woman” keyword would be:
To read more about the MET API, see here: https://metmuseum.github.io/
# the request, saved to a variable
url = f'{base_url}{path}{query}'
women = requests.get(url)
inspecting objects: two functions: type() and dir()#
Using type() and dir() to better understand our response data. The end goal is to sift through the data to discover interesting things.
# what type of object do we have?
type(women)
requests.models.Response
What can we do with a Response object? Spend a couple of minutes exploring the different methods.
dir(women)
['__attrs__',
'__bool__',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__enter__',
'__eq__',
'__exit__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__nonzero__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__setstate__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_content',
'_content_consumed',
'_next',
'apparent_encoding',
'close',
'connection',
'content',
'cookies',
'elapsed',
'encoding',
'headers',
'history',
'is_permanent_redirect',
'is_redirect',
'iter_content',
'iter_lines',
'json',
'links',
'next',
'ok',
'raise_for_status',
'raw',
'reason',
'request',
'status_code',
'text',
'url']
women.headers
{'Content-Type': 'application/json', 'Vary': 'Origin', 'X-Powered-By': 'ARR/3.0, ASP.NET', 'Date': 'Wed, 05 Nov 2025 14:43:59 GMT', 'Set-Cookie': 'visid_incap_1662004=AzsDAwU8S8mFdht29aHeUq5iC2kAAAAAQUIPAAAAAADYUY2DR4fdvhhQ2JWc5xQa; expires=Thu, 05 Nov 2026 06:37:48 GMT; HttpOnly; path=/; Domain=.metmuseum.org, incap_ses_703_1662004=BjucGHeruh9qcgSbOI/BCa9iC2kAAAAAqxcDiE2Rl2dpGKJh0bC/7A==; path=/; Domain=.metmuseum.org', 'X-CDN': 'Imperva', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'X-Iinfo': '10-13151182-13151183 NNYN CT(98 189 0) RT(1762353839351 159) q(0 0 3 0) r(9 9) U12'}
women.raw
<urllib3.response.HTTPResponse at 0x1156e5c90>
women.elapsed
datetime.timedelta(seconds=1, microseconds=472696)
women.ok
True
women
<Response [200]>
.json() to parse our response object#
# why do we need to add parenthesis?
parsed = women.json()
type(parsed)
dict
what is a dict? 2D data. We will look more closely soon.
dir(parsed)
['__class__',
'__class_getitem__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__ior__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__ne__',
'__new__',
'__or__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__ror__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'clear',
'copy',
'fromkeys',
'get',
'items',
'keys',
'pop',
'popitem',
'setdefault',
'update',
'values']
parsed.keys()
dict_keys(['total', 'objectIDs'])
We access items from a dict with brackets[].
accessing items from a dict#
What is a dictionary anyway?
instructor = {
'name': ['filipa calado', 'patrick smyth', 'stephen zweibel'],
'age': [35, 37, 38],
'degree': ['literature', 'literature', 'library science'],
'job': ['digital scholarship specialist', 'startup', 'digital scholarship librarian']
}
# see the keys
instructor.keys()
dict_keys(['name', 'age', 'degree', 'job'])
# access items through brackets containing keys
instructor['name']
['filipa calado', 'patrick smyth', 'stephen zweibel']
similar to a DataFrame in pandas#
import pandas as pd
df = pd.DataFrame(instructor)
A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.1 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.
If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.
Traceback (most recent call last): File "/Users/fcalado/.conda/envs/jb/lib/python3.11/runpy.py", line 198, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/runpy.py", line 88, in _run_code
exec(code, run_globals)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel_launcher.py", line 18, in <module>
app.launch_new_instance()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/traitlets/config/application.py", line 1075, in launch_instance
app.start()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelapp.py", line 739, in start
self.io_loop.start()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/tornado/platform/asyncio.py", line 205, in start
self.asyncio_loop.run_forever()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/asyncio/base_events.py", line 608, in run_forever
self._run_once()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/asyncio/base_events.py", line 1936, in _run_once
handle._run()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/asyncio/events.py", line 84, in _run
self._context.run(self._callback, *self._args)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
await self.process_one()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 534, in process_one
await dispatch(*args)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
await result
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/ipkernel.py", line 362, in execute_request
await super().execute_request(stream, ident, parent)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 778, in execute_request
reply_content = await reply_content
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/ipkernel.py", line 449, in do_execute
res = shell.run_cell(
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/zmqshell.py", line 549, in run_cell
return super().run_cell(*args, **kwargs)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3075, in run_cell
result = self._run_cell(
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3130, in _run_cell
result = runner(coro)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/async_helpers.py", line 128, in _pseudo_sync_runner
coro.send(None)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3334, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3517, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "/var/folders/z6/8hbw1n6n3dq2jdw3_zjysj5sws21gh/T/ipykernel_71578/3454732655.py", line 1, in <module>
import pandas as pd
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/pandas/__init__.py", line 26, in <module>
from pandas.compat import (
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/pandas/compat/__init__.py", line 27, in <module>
from pandas.compat.pyarrow import (
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/pandas/compat/pyarrow.py", line 8, in <module>
import pyarrow as pa
File "/Users/fcalado/.local/lib/python3.11/site-packages/pyarrow/__init__.py", line 65, in <module>
import pyarrow.lib as _lib
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: _ARRAY_API not found
A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.1 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.
If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.
Traceback (most recent call last): File "/Users/fcalado/.conda/envs/jb/lib/python3.11/runpy.py", line 198, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/runpy.py", line 88, in _run_code
exec(code, run_globals)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel_launcher.py", line 18, in <module>
app.launch_new_instance()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/traitlets/config/application.py", line 1075, in launch_instance
app.start()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelapp.py", line 739, in start
self.io_loop.start()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/tornado/platform/asyncio.py", line 205, in start
self.asyncio_loop.run_forever()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/asyncio/base_events.py", line 608, in run_forever
self._run_once()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/asyncio/base_events.py", line 1936, in _run_once
handle._run()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/asyncio/events.py", line 84, in _run
self._context.run(self._callback, *self._args)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
await self.process_one()
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 534, in process_one
await dispatch(*args)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
await result
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/ipkernel.py", line 362, in execute_request
await super().execute_request(stream, ident, parent)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 778, in execute_request
reply_content = await reply_content
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/ipkernel.py", line 449, in do_execute
res = shell.run_cell(
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/ipykernel/zmqshell.py", line 549, in run_cell
return super().run_cell(*args, **kwargs)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3075, in run_cell
result = self._run_cell(
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3130, in _run_cell
result = runner(coro)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/async_helpers.py", line 128, in _pseudo_sync_runner
coro.send(None)
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3334, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3517, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "/var/folders/z6/8hbw1n6n3dq2jdw3_zjysj5sws21gh/T/ipykernel_71578/3454732655.py", line 1, in <module>
import pandas as pd
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/pandas/__init__.py", line 49, in <module>
from pandas.core.api import (
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/pandas/core/api.py", line 9, in <module>
from pandas.core.dtypes.dtypes import (
File "/Users/fcalado/.conda/envs/jb/lib/python3.11/site-packages/pandas/core/dtypes/dtypes.py", line 24, in <module>
from pandas._libs import (
File "/Users/fcalado/.local/lib/python3.11/site-packages/pyarrow/__init__.py", line 65, in <module>
import pyarrow.lib as _lib
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: _ARRAY_API not found
df
| name | age | degree | job | |
|---|---|---|---|---|
| 0 | filipa calado | 35 | literature | digital scholarship specialist |
| 1 | patrick smyth | 37 | literature | startup |
| 2 | stephen zweibel | 38 | library science | digital scholarship librarian |
# let's try with the first object
parsed.keys()
dict_keys(['total', 'objectIDs'])
We access items from a dict using brackets.
# just printing out the first ten
parsed['objectIDs'][:10]
[483873,
207162,
834487,
685333,
483877,
494305,
481438,
374147,
489510,
685319]
type(parsed['objectIDs'])
list
parsed['objectIDs'][0]
483873
individual challenge:#
Reading the docs for the “Search” endpoint, play around with the different parameters. Try do searches for isHighlight, or dateBegin and dateEnd. You can layer parameters by using the & character in between them.
using the “objects” endpoint#
So far, we have been using the “search” endpoint for our requests. Now, we will create a new request using the “objects” endpoint”. with data from the original request
Now we past the URL for “object” (rather than search), and we do it within an f-string, putting the variable “first” into curly brackets at the end.
first = parsed['objectIDs'][0]
path = '/public/collection/v1/objects/'
# the base_url stays the same, so we don't need to change it.
url = f"{base_url}{path}{first}"
# running the request
first_object = requests.get(url)
# checking the resulting object
first_object
<Response [200]>
dir(first_object)
['__attrs__',
'__bool__',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__enter__',
'__eq__',
'__exit__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__nonzero__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__setstate__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_content',
'_content_consumed',
'_next',
'apparent_encoding',
'close',
'connection',
'content',
'cookies',
'elapsed',
'encoding',
'headers',
'history',
'is_permanent_redirect',
'is_redirect',
'iter_content',
'iter_lines',
'json',
'links',
'next',
'ok',
'raise_for_status',
'raw',
'reason',
'request',
'status_code',
'text',
'url']
look at the json for the first object
# all of the data about this object
# look at the URL!
first_object.json()
{'objectID': 483873,
'isHighlight': False,
'accessionNumber': '1984.613.2',
'accessionYear': '1984',
'isPublicDomain': False,
'primaryImage': '',
'primaryImageSmall': '',
'additionalImages': [],
'constituents': [{'constituentID': 161817,
'role': 'Artist',
'name': 'Willem de Kooning',
'constituentULAN_URL': 'http://vocab.getty.edu/page/ulan/500000974',
'constituentWikidata_URL': 'https://www.wikidata.org/wiki/Q132305',
'gender': ''}],
'department': 'Modern and Contemporary Art',
'objectName': 'Painting',
'title': 'Woman',
'culture': '',
'period': '',
'dynasty': '',
'reign': '',
'portfolio': '',
'artistRole': 'Artist',
'artistPrefix': '',
'artistDisplayName': 'Willem de Kooning',
'artistDisplayBio': 'American (born The Netherlands), Rotterdam 1904–1997 East Hampton, New York',
'artistSuffix': '',
'artistAlphaSort': 'De Kooning, Willem',
'artistNationality': 'American, born The Netherlands',
'artistBeginDate': '1904',
'artistEndDate': '1997',
'artistGender': '',
'artistWikidata_URL': 'https://www.wikidata.org/wiki/Q132305',
'artistULAN_URL': 'http://vocab.getty.edu/page/ulan/500000974',
'objectDate': '1944',
'objectBeginDate': 1944,
'objectEndDate': 1944,
'medium': 'Oil and charcoal on canvas',
'dimensions': '45 7/8 Ă— 31 3/4 in. (116.5 Ă— 80.6 cm)',
'measurements': [{'elementName': 'Overall',
'elementDescription': None,
'elementMeasurements': {'Height': 116.522736, 'Width': 80.645164}},
{'elementName': 'Framed',
'elementDescription': 'F.1.1984.613.2 Brown wood float w//AR plexi',
'elementMeasurements': {'Depth': 9.207519,
'Height': 125.730255,
'Width': 90.48768}}],
'creditLine': 'From the Collection of Thomas B. Hess, Gift of the heirs of Thomas B. Hess, 1984',
'geographyType': '',
'city': '',
'state': '',
'county': '',
'country': '',
'region': '',
'subregion': '',
'locale': '',
'locus': '',
'excavation': '',
'river': '',
'classification': 'Paintings',
'rightsAndReproduction': '© 2025 Artists Rights Society (ARS), New York',
'linkResource': '',
'metadataDate': '2025-10-02T04:53:22.577Z',
'repository': 'Metropolitan Museum of Art, New York, NY',
'objectURL': 'https://www.metmuseum.org/art/collection/search/483873',
'tags': [{'term': 'Women',
'AAT_URL': 'http://vocab.getty.edu/page/aat/300025943',
'Wikidata_URL': 'https://www.wikidata.org/wiki/Q467'}],
'objectWikidata_URL': 'https://www.wikidata.org/wiki/Q20189686',
'isTimelineWork': False,
'GalleryNumber': ''}
# now we can save this first object to its own variable.
# will make it easier to do more things to it!
first_json = first_object.json()
individual practice:#
Take a few minutes to inspect the dataset by using different keys.
Why do you think there’s no gender?
# no result!
first_json['artistGender']
''
first_json['department']
'Modern and Contemporary Art'
first_json['culture']
''
looping through our objects#
We are going to create a larger collection of object information (the metadata about each art object in the search results), so that we can loop through the objects to find out more about the artist’s gender.
making a list of ids#
# first, print out the first 10 items in the objectIDs list
parsed['objectIDs'][:10]
[483873,
207162,
834487,
685333,
483877,
494305,
481438,
374147,
489510,
685319]
ids = parsed['objectIDs'][:10]
# remember we can slice our list, to get just the first ten
for item in ids:
print(item)
483873
207162
834487
685333
483877
494305
481438
374147
489510
685319
type(ids)
list
len(ids)
10
Now let’s go back to the original list, and pull out all the info for the results.
making a new request#
Remember how to make a new request, using an f-string
url = f'{base_url}{path}{item}
Now, add that request to your loop so you make a new request for the first ten items on the list of object IDs. Once you get the response, parse it (with json()) and append that item to a new list.
first_ten = []
for item in ids:
# passing the objectID variable into the URL
base_url = 'https://collectionapi.metmuseum.org'
path = '/public/collection/v1/objects/'
url = f'{base_url}{path}{item}'
# grabbing our response for that object
response = requests.get(url)
# parsing our response with json
parsed = response.json()
# appending the response to our new list
first_ten.append(parsed)
# because we already know the first, let's check the last item
first_ten[-1]
{'message': 'Not a valid object'}
now guess what type of data we have for first_ten?
type(first_ten)
list
group challenge:#
Write a loop to print out information about some of the keys for each object. For example, print out the value of artistDisplayName.
for item in first_ten:
print(item['artistDisplayName'])
Willem de Kooning
Royal Porcelain Manufactory, Naples
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[42], line 2
1 for item in first_ten:
----> 2 print(item['artistDisplayName'])
KeyError: 'artistDisplayName'
let’s look at some of the values
for item in first_ten:
print(item['title'])
Woman
Woman
Terracotta lekythos (oil flask)
Woman
Woman
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[39], line 2
1 for item in first_ten:
----> 2 print(item['title'])
KeyError: 'title'
There’s another way to grab items from a dict, using the get() method.
dir(item)
['__class__',
'__class_getitem__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__ior__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__ne__',
'__new__',
'__or__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__ror__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'clear',
'copy',
'fromkeys',
'get',
'items',
'keys',
'pop',
'popitem',
'setdefault',
'update',
'values']
for item in first_ten:
if item.get('artistDisplayName'):
print(item['artistDisplayName'])
Willem de Kooning
Royal Porcelain Manufactory, Naples
Woman Painter
Man Ray
Willem de Kooning
Joan MirĂł
Willem de Kooning
Harold Anchel
Alexander Calder
for item in first_ten:
title = item.get('artistDisplayName')
print(title)
Willem de Kooning
Royal Porcelain Manufactory, Naples
Woman Painter
Man Ray
Willem de Kooning
None
Joan MirĂł
Willem de Kooning
Harold Anchel
Alexander Calder
about “women”#
Let’s make our dataset bigger, with 50 works.
first_fifty = []
for item in parsed['objectIDs'][:50]:
url = f'https://collectionapi.metmuseum.org/public/collection/v1/objects/{item}'
response = requests.get(url)
parsed = response.json()
first_fifty.append(parsed)
# combine get() with conditional to get rid of the None's
for item in first_fifty:
if item.get('artistDisplayName'):
print(item['artistDisplayName'])
# or, if we want to show the value, even if empty or None
for item in first_hundred:
gender = item.get('artistGender')
print(gender)
None
None
None
None
Female
Female
Female
Let’s use get() combined with f-strings to print both the name and the gender.
# why do you think we see only female?
for item in first_fifty:
if item.get('artistDisplayName'):
print(f"artist name: {item['artistDisplayName']}")
print(f"artist gender: {item['artistGender']}")
artist name: Willem de Kooning
artist gender:
artist name: Royal Porcelain Manufactory, Naples
artist gender:
artist name: Woman Painter
artist gender:
artist name: Man Ray
artist gender:
artist name: Willem de Kooning
artist gender:
artist name: Joan MirĂł
artist gender:
artist name: Willem de Kooning
artist gender:
artist name: Alexander Calder
artist gender:
artist name: Harold Anchel
artist gender:
artist name: Malick Sidibé
artist gender:
artist name: Joan MirĂł
artist gender:
artist name: Joan MirĂł
artist gender:
artist name: Lorenzo Mosca
artist gender:
artist name: Bow Porcelain Factory
artist gender:
artist name: Royal Porcelain Manufactory, Berlin
artist gender:
artist name: Vienna
artist gender:
artist name: Edgar Degas
artist gender:
artist name: Gaston Lachaise
artist gender:
artist name: Fernand Léger
artist gender:
artist name: Edgar Degas
artist gender:
artist name: Frans Hals
artist gender:
artist name: Jean Dubuffet
artist gender:
artist name: Edgar Degas
artist gender:
artist name: Fra Filippo Lippi
artist gender:
artist name: Rembrandt (Rembrandt van Rijn)
artist gender:
artist name: Gustave Courbet
artist gender:
artist name: Quinten Massys
artist gender:
artist name: Jean-François Millet
artist gender:
artist name: Henri Fantin-Latour
artist gender:
artist name: Charles-Henri-Joseph Cordier
artist gender:
artist name: Louis Anquetin
artist gender:
artist name: Pablo Picasso
artist gender:
artist name: Bastis Master
artist gender:
artist name: Ruth Ava Lyons
artist gender: Female
artist name: Christian Julia
artist gender:
artist name: Johannes Vermeer
artist gender:
artist name: Georges Braque
artist gender:
artist name: Gustave Courbet
artist gender:
artist name: Johannes Vermeer
artist gender:
artist name: Orazio Borgianni
artist gender:
artist name: Antoine Watteau
artist gender:
artist name: Johannes Vermeer
artist gender:
artist name: Berthe Morisot
artist gender: Female
artist name: Charles-Henri-Joseph Cordier
artist gender:
artist name: Gustave Courbet
artist gender:
artist name: Louise Bourgeois
artist gender: Female
artist name: Edgar Degas
artist gender:
artist name: Rembrandt (Rembrandt van Rijn)
artist gender:
artist name: Petrus Christus
artist gender:
artist name: Léon Bonnat
artist gender:
artist name: Pablo Picasso
artist gender:
artist name: Master of Rimini
artist gender:
saving to DataFrame#
# look again at the first object
first_fifty[0]
{'objectID': 483873,
'isHighlight': False,
'accessionNumber': '1984.613.2',
'accessionYear': '1984',
'isPublicDomain': False,
'primaryImage': '',
'primaryImageSmall': '',
'additionalImages': [],
'constituents': [{'constituentID': 161817,
'role': 'Artist',
'name': 'Willem de Kooning',
'constituentULAN_URL': 'http://vocab.getty.edu/page/ulan/500000974',
'constituentWikidata_URL': 'https://www.wikidata.org/wiki/Q132305',
'gender': ''}],
'department': 'Modern and Contemporary Art',
'objectName': 'Painting',
'title': 'Woman',
'culture': '',
'period': '',
'dynasty': '',
'reign': '',
'portfolio': '',
'artistRole': 'Artist',
'artistPrefix': '',
'artistDisplayName': 'Willem de Kooning',
'artistDisplayBio': 'American (born The Netherlands), Rotterdam 1904–1997 East Hampton, New York',
'artistSuffix': '',
'artistAlphaSort': 'De Kooning, Willem',
'artistNationality': 'American, born The Netherlands',
'artistBeginDate': '1904',
'artistEndDate': '1997',
'artistGender': '',
'artistWikidata_URL': 'https://www.wikidata.org/wiki/Q132305',
'artistULAN_URL': 'http://vocab.getty.edu/page/ulan/500000974',
'objectDate': '1944',
'objectBeginDate': 1944,
'objectEndDate': 1944,
'medium': 'Oil and charcoal on canvas',
'dimensions': '45 7/8 Ă— 31 3/4 in. (116.5 Ă— 80.6 cm)',
'measurements': [{'elementName': 'Overall',
'elementDescription': None,
'elementMeasurements': {'Height': 116.522736, 'Width': 80.645164}},
{'elementName': 'Frame',
'elementDescription': 'F.1.1984.613.2 Brown wood float frame-AR Pexi',
'elementMeasurements': {'Depth': 9.207519,
'Height': 125.730255,
'Width': 90.80518}}],
'creditLine': 'From the Collection of Thomas B. Hess, Gift of the heirs of Thomas B. Hess, 1984',
'geographyType': '',
'city': '',
'state': '',
'county': '',
'country': '',
'region': '',
'subregion': '',
'locale': '',
'locus': '',
'excavation': '',
'river': '',
'classification': 'Paintings',
'rightsAndReproduction': '© 2024 Artists Rights Society (ARS), New York',
'linkResource': '',
'metadataDate': '2024-09-21T04:53:05.127Z',
'repository': 'Metropolitan Museum of Art, New York, NY',
'objectURL': 'https://www.metmuseum.org/art/collection/search/483873',
'tags': [{'term': 'Women',
'AAT_URL': 'http://vocab.getty.edu/page/aat/300025943',
'Wikidata_URL': 'https://www.wikidata.org/wiki/Q467'}],
'objectWikidata_URL': 'https://www.wikidata.org/wiki/Q20189686',
'isTimelineWork': False,
'GalleryNumber': ''}
# let's get a bunch of this data into lists
titles = []
names = []
genders = []
depts = []
countries = []
urls = []
for item in first_fifty:
title = item.get('artistGender')
titles.append(title)
name = item.get('artistDisplayName')
names.append(name)
gender = item.get('artistGender')
genders.append(gender)
dept = item.get('department')
depts.append(dept)
country = item.get('country')
countries.append(country)
url = item.get('objectURL')
urls.append(url)
countries
['',
'',
'',
'',
'',
None,
'',
'',
None,
'',
'',
'Mali',
'',
'',
None,
'',
'',
'',
'',
'',
'',
'',
'',
'',
None,
'Egypt',
'Egypt',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'Egypt',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'France',
'',
'Egypt',
'',
'Egypt',
'',
'',
'',
'',
'',
'',
'',
'Turkey',
'',
'',
'Byzantine Egypt',
'Egypt',
'',
'',
'Egypt',
'',
'',
'',
'',
'Egypt',
'',
'',
'',
'',
'',
'',
'Egypt',
'Egypt',
'',
'Egypt',
'Egypt',
'Egypt',
'',
'Egypt',
'Egypt',
'',
'Egypt',
'',
'Egypt',
'',
'',
'',
'Egypt']
depts
['Modern and Contemporary Art',
'European Sculpture and Decorative Arts',
'Greek and Roman Art',
'Photographs',
'Modern and Contemporary Art',
None,
'Modern and Contemporary Art',
'Modern and Contemporary Art',
None,
'Modern and Contemporary Art',
'Drawings and Prints',
'The Michael C. Rockefeller Wing',
'Modern and Contemporary Art',
'Modern and Contemporary Art',
None,
'European Sculpture and Decorative Arts',
'European Sculpture and Decorative Arts',
'European Sculpture and Decorative Arts',
'European Sculpture and Decorative Arts',
'European Sculpture and Decorative Arts',
'European Sculpture and Decorative Arts',
'European Sculpture and Decorative Arts',
'European Paintings',
'Modern and Contemporary Art',
None,
'Egyptian Art',
'Egyptian Art',
'Greek and Roman Art',
'Modern and Contemporary Art',
'Medieval Art',
'European Sculpture and Decorative Arts',
'European Paintings',
'Greek and Roman Art',
'Greek and Roman Art',
'Egyptian Art',
'Modern and Contemporary Art',
'European Paintings',
'European Paintings',
'Egyptian Art',
'European Paintings',
'European Paintings',
'Greek and Roman Art',
'Egyptian Art',
'European Paintings',
'Greek and Roman Art',
'Greek and Roman Art',
'Greek and Roman Art',
'Greek and Roman Art',
'Greek and Roman Art',
'European Paintings',
'European Paintings',
'European Sculpture and Decorative Arts',
'Drawings and Prints',
'Modern and Contemporary Art',
'Egyptian Art',
'Greek and Roman Art',
'Egyptian Art',
'Greek and Roman Art',
'Egyptian Art',
'Greek and Roman Art',
'Greek and Roman Art',
'Greek and Roman Art',
'Drawings and Prints',
'Drawings and Prints',
'Islamic Art',
'European Paintings',
'Modern and Contemporary Art',
'Medieval Art',
'Egyptian Art',
'European Paintings',
'European Paintings',
'Egyptian Art',
'European Paintings',
'Greek and Roman Art',
'Robert Lehman Collection',
'European Paintings',
'Egyptian Art',
'European Paintings',
'European Sculpture and Decorative Arts',
'European Paintings',
'Greek and Roman Art',
'Drawings and Prints',
'European Sculpture and Decorative Arts',
'Egyptian Art',
'Egyptian Art',
'European Paintings',
'Egyptian Art',
'Egyptian Art',
'Egyptian Art',
'Robert Lehman Collection',
'Egyptian Art',
'Egyptian Art',
'European Paintings',
'Egyptian Art',
'Greek and Roman Art',
'Egyptian Art',
'Modern and Contemporary Art',
'The Cloisters',
'Medieval Art',
'Egyptian Art']
urls
['https://www.metmuseum.org/art/collection/search/483873',
'https://www.metmuseum.org/art/collection/search/207162',
'https://www.metmuseum.org/art/collection/search/247582',
'https://www.metmuseum.org/art/collection/search/283274',
'https://www.metmuseum.org/art/collection/search/483877',
None,
'https://www.metmuseum.org/art/collection/search/494305',
'https://www.metmuseum.org/art/collection/search/481438',
None,
'https://www.metmuseum.org/art/collection/search/489510',
'https://www.metmuseum.org/art/collection/search/374147',
'https://www.metmuseum.org/art/collection/search/515811',
'https://www.metmuseum.org/art/collection/search/492892',
'https://www.metmuseum.org/art/collection/search/486510',
None,
'https://www.metmuseum.org/art/collection/search/204181',
'https://www.metmuseum.org/art/collection/search/205376',
'https://www.metmuseum.org/art/collection/search/193453',
'https://www.metmuseum.org/art/collection/search/195502',
'https://www.metmuseum.org/art/collection/search/193045',
'https://www.metmuseum.org/art/collection/search/200897',
'https://www.metmuseum.org/art/collection/search/208482',
'https://www.metmuseum.org/art/collection/search/436173',
'https://www.metmuseum.org/art/collection/search/483484',
None,
'https://www.metmuseum.org/art/collection/search/548584',
'https://www.metmuseum.org/art/collection/search/551500',
'https://www.metmuseum.org/art/collection/search/253505',
'https://www.metmuseum.org/art/collection/search/486423',
'https://www.metmuseum.org/art/collection/search/467700',
'https://www.metmuseum.org/art/collection/search/196462',
'https://www.metmuseum.org/art/collection/search/436616',
'https://www.metmuseum.org/art/collection/search/255391',
'https://www.metmuseum.org/art/collection/search/251428',
'https://www.metmuseum.org/art/collection/search/547202',
'https://www.metmuseum.org/art/collection/search/486578',
'https://www.metmuseum.org/art/collection/search/436121',
'https://www.metmuseum.org/art/collection/search/436896',
'https://www.metmuseum.org/art/collection/search/547334',
'https://www.metmuseum.org/art/collection/search/437402',
'https://www.metmuseum.org/art/collection/search/436002',
'https://www.metmuseum.org/art/collection/search/254469',
'https://www.metmuseum.org/art/collection/search/560727',
'https://www.metmuseum.org/art/collection/search/436986',
'https://www.metmuseum.org/art/collection/search/254597',
'https://www.metmuseum.org/art/collection/search/253135',
'https://www.metmuseum.org/art/collection/search/249222',
'https://www.metmuseum.org/art/collection/search/253050',
'https://www.metmuseum.org/art/collection/search/248132',
'https://www.metmuseum.org/art/collection/search/437099',
'https://www.metmuseum.org/art/collection/search/436295',
'https://www.metmuseum.org/art/collection/search/232047',
'https://www.metmuseum.org/art/collection/search/334249',
'https://www.metmuseum.org/art/collection/search/486845',
'https://www.metmuseum.org/art/collection/search/544210',
'https://www.metmuseum.org/art/collection/search/252452',
'https://www.metmuseum.org/art/collection/search/587759',
'https://www.metmuseum.org/art/collection/search/249091',
'https://www.metmuseum.org/art/collection/search/547257',
'https://www.metmuseum.org/art/collection/search/255417',
'https://www.metmuseum.org/art/collection/search/255275',
'https://www.metmuseum.org/art/collection/search/248689',
'https://www.metmuseum.org/art/collection/search/389226',
'https://www.metmuseum.org/art/collection/search/839042',
'https://www.metmuseum.org/art/collection/search/451943',
'https://www.metmuseum.org/art/collection/search/437881',
'https://www.metmuseum.org/art/collection/search/486742',
'https://www.metmuseum.org/art/collection/search/464456',
'https://www.metmuseum.org/art/collection/search/550970',
'https://www.metmuseum.org/art/collection/search/436024',
'https://www.metmuseum.org/art/collection/search/437879',
'https://www.metmuseum.org/art/collection/search/544689',
'https://www.metmuseum.org/art/collection/search/441024',
'https://www.metmuseum.org/art/collection/search/247004',
'https://www.metmuseum.org/art/collection/search/459192',
'https://www.metmuseum.org/art/collection/search/437880',
'https://www.metmuseum.org/art/collection/search/547860',
'https://www.metmuseum.org/art/collection/search/437160',
'https://www.metmuseum.org/art/collection/search/231788',
'https://www.metmuseum.org/art/collection/search/436004',
'https://www.metmuseum.org/art/collection/search/249223',
'https://www.metmuseum.org/art/collection/search/371017',
'https://www.metmuseum.org/art/collection/search/196452',
'https://www.metmuseum.org/art/collection/search/555567',
'https://www.metmuseum.org/art/collection/search/554337',
'https://www.metmuseum.org/art/collection/search/437390',
'https://www.metmuseum.org/art/collection/search/546746',
'https://www.metmuseum.org/art/collection/search/547758',
'https://www.metmuseum.org/art/collection/search/547476',
'https://www.metmuseum.org/art/collection/search/459052',
'https://www.metmuseum.org/art/collection/search/547820',
'https://www.metmuseum.org/art/collection/search/543905',
'https://www.metmuseum.org/art/collection/search/435711',
'https://www.metmuseum.org/art/collection/search/566713',
'https://www.metmuseum.org/art/collection/search/255697',
'https://www.metmuseum.org/art/collection/search/568194',
'https://www.metmuseum.org/art/collection/search/500194',
'https://www.metmuseum.org/art/collection/search/468716',
'https://www.metmuseum.org/art/collection/search/466235',
'https://www.metmuseum.org/art/collection/search/550158']
data anlaysis with pandas#
import pandas as pd
df = pd.DataFrame({
'title': titles,
'name': names,
'gender': genders,
'department': depts,
'country': countries,
'link': urls
})
df
| title | name | gender | department | country | link | |
|---|---|---|---|---|---|---|
| 0 | Willem de Kooning | Modern and Contemporary Art | https://www.metmuseum.org/art/collection/searc... | |||
| 1 | Royal Porcelain Manufactory, Naples | European Sculpture and Decorative Arts | https://www.metmuseum.org/art/collection/searc... | |||
| 2 | Woman Painter | Greek and Roman Art | https://www.metmuseum.org/art/collection/searc... | |||
| 3 | Man Ray | Photographs | https://www.metmuseum.org/art/collection/searc... | |||
| 4 | Willem de Kooning | Modern and Contemporary Art | https://www.metmuseum.org/art/collection/searc... | |||
| ... | ... | ... | ... | ... | ... | ... |
| 95 | Egyptian Art | Egypt | https://www.metmuseum.org/art/collection/searc... | |||
| 96 | Pablo Picasso | Modern and Contemporary Art | https://www.metmuseum.org/art/collection/searc... | |||
| 97 | The Cloisters | https://www.metmuseum.org/art/collection/searc... | ||||
| 98 | Master of Rimini | Medieval Art | https://www.metmuseum.org/art/collection/searc... | |||
| 99 | Egyptian Art | Egypt | https://www.metmuseum.org/art/collection/searc... |
100 rows Ă— 6 columns
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100 entries, 0 to 99
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 title 96 non-null object
1 name 96 non-null object
2 gender 96 non-null object
3 department 96 non-null object
4 country 96 non-null object
5 link 96 non-null object
dtypes: object(6)
memory usage: 4.8+ KB
df.value_counts('department')
department
Egyptian Art 21
European Paintings 18
Greek and Roman Art 18
Modern and Contemporary Art 13
European Sculpture and Decorative Arts 12
Drawings and Prints 5
Medieval Art 3
Robert Lehman Collection 2
Islamic Art 1
Photographs 1
The Cloisters 1
The Michael C. Rockefeller Wing 1
Name: count, dtype: int64
df.department.value_counts().plot(kind = 'barh')
<Axes: ylabel='department'>
df.department.value_counts().plot(kind = 'pie')
<Axes: ylabel='count'>