🙆‍♀️

Crawling with FlickrAPI

2022/04/03に公開

1. Install flickrapi package

$ pip install flickrapi

2. Get access key and access secret key

Access here

https://www.flickr.com/services/apps/create

Select Request an API Key under the Get your API key

image.png

Choose either whether you use for commercial

image.png

Input app name and what it is

image.png

Got API key and secret

image.png

3. Coding

project_dir/

$ touch flickr.py
$ mkdir apple
from sympy import sec
from flickrapi import FlickrAPI
from urllib.request import urlretrieve
from pprint import pprint
import os, time, sys

key = "YOUR_FLICKR_KEY"
secret = "YOUR_FLICKR_SECRET"
wait_time = 1

fruitname = sys.argv[1]
savedir = "./" + fruitname

flickr = FlickrAPI(key, secret, format='parsed-json')
result = flickr.photos.search(
    text = fruitname,
    per_page = 400,
    media = 'photos',
    sort = 'relevance',
    safe_search = 1,
    extras = 'url_q, licence'
)

photos = result['photos']

for i, photo in enumerate(photos['photo']):
    pprint(photo)
    url_q = photo['url_q']
    filepath = savedir + '/' + photo['id'] + '.jpg'
    if os.path.exists(filepath): continue
    urlretrieve(url_q, filepath)
    time.sleep(wait_time)

4. Run

$ python flickr.py apple

5. Success if you got data

Your explorer project_dir/apple

image.png

Your console

            .
            .
            . 
           {'farm': 1,
            'height_q': 150,
            'id': '41350049752',
            'isfamily': 0,
            'isfriend': 0,
            'ispublic': 1,
            'owner': '71627229@N03',
            'secret': 'ca16fc0c94',
            'server': '890',
            'title': 'TMP Presentations',
            'url_q': 'https://live.staticflickr.com/890/41350049752_ca16fc0c94_q.jpg',
            'width_q': 150},
           {'farm': 1,
            'height_q': 150,
            'id': '39584115280',
            'isfamily': 0,
            'isfriend': 0,
            'ispublic': 1,
            'owner': '71627229@N03',
            'secret': 'b3e27d01f0',
            'server': '787',
            'title': 'TMP Presentations',
            'url_q': 'https://live.staticflickr.com/787/39584115280_b3e27d01f0_q.jpg',
            'width_q': 150},
           {'farm': 1,
            'height_q': 150,
            'id': '39584079680',
            'isfamily': 0,
            'isfriend': 0,
            'ispublic': 1,
            'owner': '71627229@N03',
            'secret': '73edc16e90',
            'server': '784',
            'title': 'TMP Presentations',
            'url_q': 'https://live.staticflickr.com/784/39584079680_73edc16e90_q.jpg',
            'width_q': 150},
           {'farm': 1,
            'height_q': 150,
            'id': '27523505068',
            'isfamily': 0,
            'isfriend': 0,
            'ispublic': 1,
            'owner': '71627229@N03',
            'secret': '271407bc41',
            'server': '899',
            'title': 'TMP Presentations',
            'url_q': 'https://live.staticflickr.com/899/27523505068_271407bc41_q.jpg',
            'width_q': 150},
           {'farm': 1,
            'height_q': 150,
            'id': '41393092611',
            'isfamily': 0,
            'isfriend': 0,
            .
            .
            .

Discussion