首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Pixabay API

Python Pixabay API
EN

Stack Overflow用户
提问于 2019-11-30 22:22:42
回答 1查看 828关注 0票数 1

我写了一个脚本,使用Pixabay API从Pixabay下载20000张图片。但在抓取第600张图像后,问题出现了,并显示以下错误:

“文件页面第26行,在页面”true“中,文件页面第144行,在search raise ValueError(resp.text) ValueError中: ERROR 400”"c:/Users/Dell/Desktop/python-pixabay-master/python-pixabay-master/main.py",“超出有效范围。”

代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2019-12-01 00:25:54

我已经在postman中尝试过你的API,你可以看到在API中有一个参数页面,所以在你的例子中,所有的图像都是覆盖到第3页的,所以在第4页上,它给出了page is out of valid range error from pixabay。

您可以尝试使用此邮递员链接:https://www.getpostman.com/collections/2823a8aad5ea81b55342

导入它并检查它。

您可以使用exception来处理此错误。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59117040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档