Error converting jsonp to json in python with the Pixabay API response

0

I am trying to get the URL of an image to download it by command line from a jsonp pixabay answer (using its API):

This is the request:

    import jsonp2json
    import json
    import python_pixabay

    pix = python_pixabay.Pixabay('xxxxxx')

    # default image search
    img_search = pix.image_search()   


    # custom image search
   jsonp_response = pix.image_search(q = 'cats dogs',
                           lang = 'es',
                           response_group = 'high_resolution',
                           image_type = 'photo',
                           orientation = 'horizontal',
                           category = 'animals',
                           safesearch = 'true',
                           order = 'latest',
                           page = 1,
                           per_page = 3)

    print(jsonp_response)

The answer 'jsonp_response' comes in jsonp format, like this:

   {
'totalHits': 156, 
'hits': [   
    {
        'largeImageURL': 'https://pixabay.com/get/....jpg',
        'webformatHeight': 360, 
        'webformatWidth': 640, 
        'likes': 2, 
        'imageWidth': 5184, 
        'id': 3396156, 
        'user_id': 8982455, 
        'views': 280, 
        'comments': 0, 
        'pageURL': 'https://pixabay.com/es/gato-mascota-animales-blanco-3396156/', 
        'imageHeight': 2916, 
        'webformatURL': 'https://pixabay.com/get/....jpg', 
        'type': 'photo', 
        'previewHeight': 84, 
        'tags': 'gato, mascota, animales', 
        'downloads': 126, 
        'user': 'mohdrashidsmc', 
        'favorites': 2, 
        'imageSize': 974885, 
        'previewWidth': 150, 
        'userImageURL': '', 
        'previewURL': 'https://cdn.pixabay.com/photo/2018/05/13/11/40/cat-3396156_150.jpg'
    }, 
    .....
    .....
    .....
] , 'total': 156}

Actually there are several of these in the same answer but I have only put one here so it is not so extensive.

I'm also using a module called 'jsonp2json' and it only includes this function.

import sys
def convert(jsonp):
    try:
        l_index = jsonp.index('(') + 1
        r_index = jsonp.rindex(')')
    except ValueError:
        print("Input is not in a jsonp format.")
        return

    res = jsonp[l_index:r_index]
    return res

And I'm trying to do this to get the data in json

json_result = jsonp2json.convert(jsonp_response)
result = json.loads(json_result)
print(result)

And I'm getting this error: - (

$ py -3 c:/Users/ivan/Desktop/pixabay/get_img.py                              Traceback (most recent call last):
  File "c:/Users/ivan/Desktop/pixabay/get_img.py", line 30, in <module>
    json_result = jsonp2json.convert(jsonp_response)
  File "c:/Users/ivan/Desktop/pixabay/get_img.py", line 7, in convert
    l_index = jsonp.index('(') + 1
AttributeError: 'dict' object has no attribute 'index

Pes, I'm not sure if the problem is in the variable jsonp_response or in the function convert (jsonp) Any idea how to solve this? p>

Ideally, do something like this:

print(json_result['largeImageURL'][0])
print(json_result['largeImageURL'][1])
print(json_result['largeImageURL'][2])

and get an exit like this

'https://pixabay.com/get/0.jpg'
'https://pixabay.com/get/1.jpg'
'https://pixabay.com/get/2.jpg'
    
asked by Iván Rodríguez 24.05.2018 в 06:19
source

2 answers

0

I just found this that solves all the problems I had. Here I leave it in case someone else can be useful.

hits=img_search.get("hits")
images=[]
for hit in hits:
    largeImageURL = hit['largeImageURL']
    print (largeImageURL)
    images.append (largeImageURL)   
print (images) 

Source: link

Greetings to all and thanks! : -)

    
answered by 24.05.2018 / 19:14
source
0

I just found out that it is not necessary to convert the jsonp to json to get the url of the image. It is enough to do this.

url = jsonp_response['hits'][1]['largeImageURL']    
print(url)

The solution was obtained from here link

A thousand thanks to the existence of this wonderful community, whether in Spanish or English: -)

However, I still have one question. How do I know the amount of url that contains the answer? I mean, if I have this:

url = jsonp_response['hits'][n]['largeImageURL']  

How do I know the maximum value that 'n' can have?

---------------------------------- UPDATE ------------ -----------------

the Value of n (page number) is thus obtained

n_pages = len(jsonp_response['hits'])

Thanks @FJSevilla for your answer:

From this comes another question How to get all the URLs of this page?

I tried like this but this gives me the size of the 'string' of the first url that is

n_url = len(jsonp_response['hits'][0]['largeImageURL'])
    
answered by 24.05.2018 в 15:36