Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- {
- '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}
- 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
- json_result = jsonp2json.convert(jsonp_response)
- result = json.loads(json_result)
- print(result)
- $ 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
- print(json_result['largeImageURL'][0])
- print(json_result['largeImageURL'][1])
- print(json_result['largeImageURL'][2])
- 'https://pixabay.com/get/0.jpg'
- 'https://pixabay.com/get/1.jpg'
- 'https://pixabay.com/get/2.jpg'
Add Comment
Please, Sign In to add comment