Use pexels_api to search photos from Pexels.
pip install pexels-api
The following code shows you how to import the package and make a simple query to Pexels API v1. See more examples or the documentation.
# Import API class from pexels_api package
from pexels_api import API
# Type your Pexels API
PEXELS_API_KEY = 'YOUR-PEXELS-API-KEY'
# Create API object
api = API(PEXELS_API_KEY)
# Search five 'kitten' photos
api.search('kitten', page=1, results_per_page=5)
# Get photo entries
photos = api.get_entries()
# Loop the five photos
for photo in photos:
# Print photographer
print('Photographer: ', photo.photographer)
# Print url
print('Photo url: ', photo.url)
# Print original size url
print('Photo original size: ', photo.original)- search.py:
Specify a query to search photos. - popular.py:
Search popular photos. - curated.py:
Search curated photos.
Creates an instance of a API object.
| Parameter | Required | Type | Description |
|---|---|---|---|
| PEXELS_API_KEY | Yes | String | Your Pexels API key |
An instance of a API object.
Given a query requests data using Pexels API v1.
| Parameter | Required | Type | Description |
|---|---|---|---|
| query | Yes | String | The topic to search |
| photos_per_page | No. (Default value: 15) | Integer | Number of photos per page 1 minimum 80 maximum |
| page | No. (Default value: 1) | Integer | Specify the page to search |
A dictionary containing json data of the results of the query in the specified page, None if the request fails.
Requests popular data using Pexels API v1.
| Parameter | Required | Type | Description |
|---|---|---|---|
| photos_per_page | No. (Default value: 15) | Integer | Number of photos per page 1 minimum 80 maximum |
| page | No. (Default value: 1) | Integer | Specify the page to search |
A dictionary containing json data of the results of Pexels popular page in the specified page, None if the request fails.
Requests curated data using Pexels API v1.
| Parameter | Required | Type | Description |
|---|---|---|---|
| photos_per_page | No. (Default value: 15) | Integer | Number of photos per page 1 minimum 80 maximum |
| page | No. (Default value: 1) | Integer | Specify the page to search |
A dictionary containing json data of the results of Pexels curated page in the specified page, None if the request fails.
Search the next page if available.
A dictionary containing json data of the next page, None if page not found.
Search the previous page if available.
A dictionary containing json data of the previous page, None if page not found.
Creates an instance of a Photo object for each photo in the current page.
A list of Photo objects.
By default the API properties are None. When seacrh(), popular() or curated() is performed the API properties are updated.
| Property | Type | Description |
|---|---|---|
| request | Object (requests) | Current request, None if request fails |
| json | Dictionary | A dictionary with the data of the current page |
| page | Integer | Number of the page |
| total_results | Integer | Number of total results. (None in popular and curated page) |
| page_results | Integer | Number of results in the current page |
| has_next_page | Boolean | True if there is a next page else False |
| has_previous_page | Boolean | True if there is a previous page else False |
| next_page | String | Url to the next page. (None if there is no next page) |
| prev_page | String | Url to the previous page. (None if there is no previous page) |
| Parameter | Required | Type | Description |
|---|---|---|---|
| json_photo | Yes | Dictionary | A dictionary containing json data of the photo of which you want the properties |
An instance of a Photo object.
| Property | Type | Description |
|---|---|---|
| id | Integer | Unique Pexels identifier |
| width | Integer | Width of the original photo |
| height | Integer | Height of the original photo |
| photographer | String | Photo's photographer |
| url | String | Photo's photographer |
| description | String | Photo's description |
| original | String | Photo's original size url |
| compressed | String | Photo's compressed size url |
| large2x | String | Photo's large2x size url |
| large | String | Photo's large size url |
| medium | String | Photo's medium size url |
| small | String | Photo's small size url |
| portrait | String | Photo's portrait size url |
| landscape | String | Photo's landscape size url |
| tiny | String | Photo's tiny size url |
| extension | String | Photo's extension |