You can easily hit the Web API using Python requests. This time, I will search on Flickr as an example.
Install Since it is registered on PyPi, install it using pip or easy_install.
$ pip install requests
or
$ easy_install -U requests
If you don't have a Flickr account, get one as needed.
Access the following address, register the application, and obtain the API Key and SECRET. For non-commercial registration, register with Non-Commercial. https://www.flickr.com/services/apps/create/apply/
Once you have the API key and Secret, you can get the data in JSON format by setting some parameters as shown below and GET with requests.
# -*- coding: utf-8 -*-
import json
import requests
url = 'https://api.flickr.com/services/rest/'
API_KEY = 'YOUR_API_KEY'
SECRET_KEY = 'YOUR_SECRET_KEY'
query = {
'method': 'flickr.photos.search',
'api_key': API_KEY,
'text': 'sky', #Search word
'per_page': '5', #Number of data per page
'format': 'json',
'nojsoncallback': '1'
}
r = requests.get(url, params=query)
print r
print json.dumps(r.json(), sort_keys=True, indent=2)
See below for configurable parameters on flickr.photos.search
flickr.photos.search
The response of the above program is as follows (Response data does not always match because it depends on the Flickr situation)
<Response [200]>
{
"photos": {
"page": 1,
"pages": 13568371,
"perpage": 2,
"photo": [
{
"farm": 6,
"id": "14914864943",
"isfamily": 0,
"isfriend": 0,
"ispublic": 1,
"owner": "124218704@N03",
"secret": "593dc8728a",
"server": "5606",
"title": "P1010144"
},
{
"farm": 6,
"id": "15349495280",
"isfamily": 0,
"isfriend": 0,
"ispublic": 1,
"owner": "24213796@N02",
"secret": "ef0aa50b3f",
"server": "5603",
"title": "Hot Air Balloon"
}
],
"total": "27136741"
},
"stat": "ok"
}
The actual image can be obtained by creating the following format URL with the parameters obtained in the response below.
http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
Search photos using Flickr API in Python
Recommended Posts