Python 来自Flickr的随机图像

Python 来自Flickr的随机图像,python,python-3.x,flickr,Python,Python 3.x,Flickr,如何使用API和Python获取Flickr随机图像 我使用了以下Flickr API: flickr.photos.search(text,page,per_page,extras) # where: # text = "flower" (also with other words the results are very disappointing about the randomness) # per_page = 1 (I have set 1 Image per

如何使用API和Python获取Flickr随机图像

我使用了以下Flickr API:

flickr.photos.search(text,page,per_page,extras)
# where:
#       text = "flower" (also with other words the results are very disappointing about the randomness)
#       per_page = 1 (I have set 1 Image per page)
#       page = In the vast majority of cases, the number of pages found per word exceeds 100000. Therefore I set a random number between 1 and 100000
#       extras = "url_sq,url_t,url_s,url_q,url_m,url_n,url_z,url_c,url_l,url_o"
当我启动每20秒显示一幅图像的应用程序时,结果非常令人失望,因为大约每显示20幅图像,16幅总是相同的图像。 下面是整个代码:

def update_flickrImage(self):

    FLICKR_PUBLIC = 'XXXXXXXXXXXXXXXXXX'
    FLICKR_SECRET = 'XXXXXXXXXXX'
    flickr = FlickrAPI(FLICKR_PUBLIC,FLICKR_SECRET,format='parsed-json')
    random.seed()
    rand_page = random.randrange(1,100000,1)
    extras = 'url_sq,url_t,url_s,url_q,url_m,url_n,url_z,url_c,url_l,url_o'

    cats = flickr.photos.search(text="flower", page=rand_page, per_page=1, extras=extras)

    photos = cats['photos']

    pprint(photos)
    print("Page: ",rand_page)

    for image in photos['photo']:
        self.title = image['title']
        try:
            url = image['url_o']
            width = image['width_o']
            height = image['height_o']
        except:
            try:
                url = image['url_l']
                width = image['width_l']
                height = image['height_l']
            except:
                try:
                    url = image['url_c']
                    width = image['width_c']
                    height = image['height_c']
                except:
                    pass
    try:
        r = requests.get(url)
        self.pic = r.content
    except:
        pass

我尽可能地尝试了你的代码。当我对100个电话进行测试时,我只得到了3个不同的链接

当我在randrange函数中将这个数字减少到4000时,我从100个URL中得到了98个唯一的URL。整个代码如下(我的公共和机密被注释掉):

Flickr search API限制每个用户返回4000条记录

在我的Flickr里,我有超过13000张照片。当我需要在MySQL中创建一个本地可搜索数据库时,我可以一次下载100个,最多1400页。我的大部分Flickr工作都是用PHP完成的

是的,你必须玩它。搜索“flower”返回295805页。太过分了

同样在我的代码版本中,我不得不注释掉漂亮的打印。标题会用某些UTF字符将其放大。我只是想看看独特的网址

import flickrapi as fa
import random 
# import pprint as pp
import time as ti

def update_flickrImage(self):
    FLICKR_PUBLIC = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    FLICKR_SECRET = 'XXXXXXXXXXXXXXXX'
    
    flickr = fa.FlickrAPI(FLICKR_PUBLIC,FLICKR_SECRET,format='parsed-json')
    
    random.seed()
    rand_page = random.randrange(1,4000,1)
    
    extras = 'url_sq,url_t,url_s,url_q,url_m,url_n,url_z,url_c,url_l,url_o'
    
    cats = flickr.photos.search(text="flower", 
                                page=rand_page, 
                                per_page=1, 
                                extras=extras)
    photos = cats['photos']
    
   # pp.pprint(photos)
    print("Page: ",rand_page)
     
    for image in photos['photo']:
        title = image['title']
        try:
            url = image['url_o']
            width = image['width_o']
            height = image['height_o']
        except:
            try:
                url = image['url_l']
                width = image['width_l']
                height = image['height_l']
            except:
                try:
                    url = image['url_c']
                    width = image['width_c']
                    height = image['height_c']
                except:
                    pass

    self['title'] = title
    self['url'] = url
    self['width'] = width
    self['height'] = height
    
    return url 
    
imgobj = {'title':'A','url':'https','width':'0','height':'0'}

for i in range(100):
    imgurl = update_flickrImage(imgobj)
    print( imgurl)
    ti.sleep(2)