-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_query.py
More file actions
34 lines (25 loc) · 1.01 KB
/
image_query.py
File metadata and controls
34 lines (25 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import requests
import json
import config
def query(search: str, image_count: int):
api_key = config.SERPAPI_KEY
request_string = "https://serpapi.com/search.json?engine=yandex_images&text={query}&p={page}&api_key={key}"
page = 0
count = 0
items: list[dict] = []
while count < image_count:
response = requests.get(request_string.format(query=search, page=page, key=api_key)).json()
image_results = response["images_results"]
position: int = image_results[-1]["position"]
for image_result in image_results:
image_result["page"] = page
count = position
items.extend(image_results)
page += 1
print(f"page {page}; count {count}")
with open("out/image-query.json", "w+") as file:
result = { "items": items }
json.dump(result, file)
with open("out/image-query-backup.json", "w+") as file:
result = { "items": items }
json.dump(result, file)