lundi 7 décembre 2015

How to get a random link from europeana?

It must be a simple trick, but I've tried all the "random" functions in the Python API, and they didn't work. I have this code:

def extractImage (link):
    locations = extractLocation (link)
    images = {}
    for key, value in locations.items():
            temp = key.split(',')
            key = temp[0]
            r = requests.get('http://ift.tt/1NCIN9I' % key)
            r.text
            data = json.loads(r.text)
            for i in data['items']:
                    if data['totalResults'] == 0:
                            continue
                    else:
                            try:
                                    images.update({(key):(i['edmPreview'])})
                                    break

                            except KeyError:
                                    continue
    return images

This code makes a search on europeana to get random images of specific locations that I gave int the "key", then I put it in a dictionary.

What I'm missing is the random part: when I run this code, it always gives the same links on that key that I searched, and I want the program to gave me random links each time I run it.

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire