Guest User

Untitled

a guest
May 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import jsonp2json
  2. import json
  3. import python_pixabay
  4.  
  5. pix = python_pixabay.Pixabay('xxxxxx')
  6.  
  7. # default image search
  8. img_search = pix.image_search()
  9.  
  10.  
  11. # custom image search
  12. jsonp_response = pix.image_search(q = 'cats dogs',
  13. lang = 'es',
  14. response_group = 'high_resolution',
  15. image_type = 'photo',
  16. orientation = 'horizontal',
  17. category = 'animals',
  18. safesearch = 'true',
  19. order = 'latest',
  20. page = 1,
  21. per_page = 3)
  22.  
  23. print(jsonp_response)
  24.  
  25. {
  26. 'totalHits': 156,
  27. 'hits': [
  28. {
  29. 'largeImageURL': 'https://pixabay.com/get/....jpg',
  30. 'webformatHeight': 360,
  31. 'webformatWidth': 640,
  32. 'likes': 2,
  33. 'imageWidth': 5184,
  34. 'id': 3396156,
  35. 'user_id': 8982455,
  36. 'views': 280,
  37. 'comments': 0,
  38. 'pageURL': 'https://pixabay.com/es/gato-mascota-animales-blanco-3396156/',
  39. 'imageHeight': 2916,
  40. 'webformatURL': 'https://pixabay.com/get/....jpg',
  41. 'type': 'photo',
  42. 'previewHeight': 84,
  43. 'tags': 'gato, mascota, animales',
  44. 'downloads': 126,
  45. 'user': 'mohdrashidsmc',
  46. 'favorites': 2,
  47. 'imageSize': 974885,
  48. 'previewWidth': 150,
  49. 'userImageURL': '',
  50. 'previewURL': 'https://cdn.pixabay.com/photo/2018/05/13/11/40/cat-3396156_150.jpg'
  51. },
  52. .....
  53. .....
  54. .....
  55. ] , 'total': 156}
  56.  
  57. import sys
  58. def convert(jsonp):
  59. try:
  60. l_index = jsonp.index('(') + 1
  61. r_index = jsonp.rindex(')')
  62. except ValueError:
  63. print("Input is not in a jsonp format.")
  64. return
  65.  
  66. res = jsonp[l_index:r_index]
  67. return res
  68.  
  69. json_result = jsonp2json.convert(jsonp_response)
  70. result = json.loads(json_result)
  71. print(result)
  72.  
  73. $ py -3 c:/Users/ivan/Desktop/pixabay/get_img.py Traceback (most recent call last):
  74. File "c:/Users/ivan/Desktop/pixabay/get_img.py", line 30, in <module>
  75. json_result = jsonp2json.convert(jsonp_response)
  76. File "c:/Users/ivan/Desktop/pixabay/get_img.py", line 7, in convert
  77. l_index = jsonp.index('(') + 1
  78. AttributeError: 'dict' object has no attribute 'index
  79.  
  80. print(json_result['largeImageURL'][0])
  81. print(json_result['largeImageURL'][1])
  82. print(json_result['largeImageURL'][2])
  83.  
  84. 'https://pixabay.com/get/0.jpg'
  85. 'https://pixabay.com/get/1.jpg'
  86. 'https://pixabay.com/get/2.jpg'
Add Comment
Please, Sign In to add comment