Pakshadow
Pakshadow

Reputation: 21

Python Pixabay API

I write a script to download the 20000 images from Pixabay using Pixabay API. But the problem arises after scraping the 600th images and shows the following error:

"File "c:/Users/Dell/Desktop/python-pixabay-master/python-pixabay-master/main.py", line 26, in pretty="true" File "c:\Users\Dell\Desktop\python-pixabay-master\python-pixabay-master\pixabay.py", line 144, in search raise ValueError(resp.text) ValueError: [ERROR 400] "page" is out of valid range."

Code:

from pixabay import Image, Video
import pprint
import requests
import shutil

API_KEY = 'myAPIkeys'
image = Image(API_KEY)

j=1
for n in range(1,100):
    ims = image.search(
                q="education",
                lang="en",
                image_type="all",
                orientation="all",
                category="education",
                min_width=0,
                min_height=0,
                colors="",
                editors_choice="false",
                safesearch="false",
                order="popular",
                page=n,
                per_page=200,
                callback="",
                pretty="true"

                )

#hits=ims['total']
#print(hits)    
#print(ims)

    #pp=pprint.PrettyPrinter(indent=4)
    for i in range(0,200):
        payload=ims['hits'][i]['largeImageURL']
        resp = requests.get(payload, stream=True)
        local_file = open(str(j)+"local_image.jpg", 'wb')
        resp.raw.decode_content = True
        shutil.copyfileobj(resp.raw, local_file)
        del resp



        print(str(j)+"URL of image: {}".format(payload))
        j=j+1 
        #urllib.request.urlretrieve(payload,i)

    #pp.pprint(ims)

Upvotes: 1

Views: 2016

Answers (1)

Dhruv Rajkotia
Dhruv Rajkotia

Reputation: 380

@Pakshadow Hi I have tried your API in postman, You can see that in the API there is the one parameter page, So In your case your all the images are cover till page 3 so that on page 4 it's giving page is out of valid range error from pixabay.

you can try with this postman link: https://www.getpostman.com/collections/2823a8aad5ea81b55342

Import it and check that.

You can handle this error using exception.

from pixabay import Image
import requests
import shutil

API_KEY = 'API_KEY'
image = Image(API_KEY)

j=1
for n in range(1,100):
    try:
        ims = image.search(
                    q="education",
                    lang="en",
                    image_type="all",
                    orientation="all",
                    category="education",
                    min_width=0,
                    min_height=0,
                    colors="",
                    editors_choice="false",
                    safesearch="false",
                    order="popular",
                    page=n,
                    per_page=200,
                    callback="",
                    pretty="true"

                    )
        for i in range(0, 200):
            payload = ims['hits'][i]['largeImageURL']
            resp = requests.get(payload, stream=True)
            local_file = open(str(j) + "local_image.jpg", 'wb')
            resp.raw.decode_content = True
            shutil.copyfileobj(resp.raw, local_file)
            del resp

            print(str(j) + "URL of image: {}".format(payload))
            j = j + 1
            # urllib.request.urlretrieve(payload,i)

        # pp.pprint(ims)

    except Exception as e:
        print(e)

Upvotes: 0

Related Questions