简体   繁体   English

如何从Python 3中的Flickr API下载Flickr中的照片

[英]How to download photos from Flickr by Flickr API in Python 3

noob question series... noob问题系列......

I am a new learner of python, recently want to create a small python application that can collect photos from flickr based on different search input. 我是python的新学习者,最近想创建一个小的python应用程序,可以根据不同的搜索输入从flickr收集照片。 (eg: if i input "dog", it will download all dog images from flickr) (例如:如果我输入“dog”,它将从flickr下载所有狗图像)

I did some research online and notice that flickr API might be the best way and the method flickr.photos.getSizes should be the one I need to use. 我在网上进行了一些研究,并注意到flickr API可能是最好的方法,方法flickr.photos.getSizes应该是我需要使用的方法。

However, I have few stupid questions when coding: 但是,编码时我几乎没有愚蠢的问题:

  1. I have applied my key and secret for flickr API, I just don't know what to do next with flickr.photos.getSizes in python to download photos. 我已经为flickr API应用了我的密钥和秘密,我只是不知道下一步用python中的flickr.photos.getSizes来下载照片。 Like, how to call this method in python? 比如,如何在python中调用此方法? (and I noticed required arguments for this method are keys and photo_id, how to get photo_ids based on search input "dog") (我注意到这个方法所需的参数是keys和photo_id,如何基于搜索输入“dog”获得photo_ids)

  2. Then I followed a tutorial from https://github.com/alexis-mignon/python-flickr-api/wiki/Tutorial but when I imported flickr_api I got error message: 然后我按照https://github.com/alexis-mignon/python-flickr-api/wiki/Tutorial的教程,但是当我导入flickr_api时收到错误消息:

     Could not load all modules <class 'ImportError'> No module named 'objects' Traceback (most recent call last): File "D:/Agfa/Projects/Image/flickr.py", line 2, in <module> import flickr_api File "D:\\Application\\Anaconda3\\lib\\site-packages\\flickr_api\\__init__.py", line 32, in <module> from auth import set_auth_handler ImportError: cannot import name 'set_auth_handler' 

    Then I took a look at the _ init _.py: 然后我看了一下_ init _.py:

     try: from objects import * import objects import upload as Upload from upload import upload, replace except Exception as e: print "Could not load all modules" print type(e), e from auth import set_auth_handler from method_call import enable_cache, disable_cache from keys import set_keys from _version import __version__ 

    Seems like this library does not support python 3 but I don't know what to do. 好像这个库不支持python 3,但我不知道该怎么做。 (I cannot install methond_call, keys, _version on my python 3) guess I will use flickrapi (我不能在我的python 3上安装methond_call,keys,_version )猜我会用flickrapi

Thank you so much for your time and again thanks in advance. 非常感谢您一次又一次地感谢您。

I think I finally got the proper way to use FlickrAPI: 我想我终于有了使用FlickrAPI的正确方法:

there are many ways but I figured out 2: 有很多方法,但我想出2:

def flickr_walk(keyward):
    count = 0
    photos = flickr.walk(text=keyward,
                 tag_mode='all',
                 tags=keyward,
                 extras='url_c',
                 per_page=100)

    for photo in photos:
        try:
            url=photo.get('url_c')
            urllib.request.urlretrieve(url, path+'\\' + str(count) +".jpg")
        except Exception as e:
            print('failed to download image')

flickr.walk uses Photos.search API, I can use the API directly as well: flickr.walk使用Photos.search API,我也可以直接使用API​​:

def flickr_search(keyward):
    obj = flickr.photos.search(text=keyward,
                           tags=keyward,
                           extras='url_c',
                           per_page=5)

    for photo in obj:
        url=photo.get('url_c')
        photos = ET.dump(obj)
        print (photos)

Remember to get the key and secret first: 记得首先获得密钥和秘密:

api_key = 'xxxxxxxxxxxxxxxx'
api_secret = 'xxxxxxxxxxxxx'

flickr=flickrapi.FlickrAPI(api_key,api_secret,cache=True)

I dont have any clue on the why/how. 我对于为什么/如何没有任何线索。 If you want to use the flickr_api module with python3.5+, you need to fix the Imports, like I did below: 如果你想使用python3.5 +的flickr_api模块,你需要修复Imports,就像我在下面做的那样:

try:
    from objects import *
    import objects
    import upload as Upload
    from upload import upload, replace
except Exception as e:
    #print "Could not load all modules"
    print( type(e), e)

from .auth import set_auth_handler
from .method_call import enable_cache, disable_cache
from .keys import set_keys
from ._version import __version__

After this edit, it fails with another Import Error on: 在此编辑之后,它失败并出现另一个导入错误:

>>> import flickr_api
<class 'SyntaxError'> invalid syntax (method_call.py, line 50)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/krysopath/.local/lib/python3.5/site-packages/flickr_api/__init__.py", line 32, in <module>
    from .auth import set_auth_handler
  File "/home/krysopath/.local/lib/python3.5/site-packages/flickr_api/auth.py", line 43, in <module>
    import urlparse
ImportError: No module named 'urlparse'

So you can fix this by yourself, if you want to, by just walking along the Import Errors and adding a dot to convert them into absolute Imports, that dont fail. 因此,如果您愿意,您可以自己解决这个问题,只需沿着导入错误并添加一个点将它们转换为绝对导入,这不会失败。

I guess, if you want to use this modul you have to fix it first... and have an unknown return. 我想,如果你想使用这个模块,你必须先修复它...并且有一个未知的回报。 So if you didnt already invested heavily, it might be more effective to use that other module. 因此,如果您没有投入大量资金,那么使用其他模块可能会更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM