我写了一个脚本,使用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",“超出有效范围。”
代码:
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)发布于 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来处理此错误。
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)https://stackoverflow.com/questions/59117040
复制相似问题