-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicture.py
More file actions
67 lines (48 loc) · 1.48 KB
/
Picture.py
File metadata and controls
67 lines (48 loc) · 1.48 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import time
import os
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
def get_img(img_link, name, index):
if not os.path.isdir(name):
os.mkdir(name)
picture = requests.get(img_link)
saver = open(os.path.join(f"{name}/{str(index).zfill(4)}.jpg"), "wb")
saver.write(picture.content)
saver.close()
def download_img(path, key) -> None:
os.chdir(path)
if not os.path.isdir("dataset"):
os.mkdir("dataset")
os.chdir("dataset")
count = 0
page = 0
while (count < 1000):
key1=key.replace(" ", "%20")
url = f"https://yandex.ru/images/search?p={page}&text={key1}"
driver = webdriver.Chrome()
driver.get(url = url)
time.sleep(5)
try:
_ = driver.find_elements( By.CLASS_NAME, 'CheckboxCaptcha')
input('Press enter after the captcha appears')
driver.get(url = url)
time.sleep(5)
except Exception as e:
print('Captcha missing')
imgs = driver.find_elements( By.CLASS_NAME, 'SimpleImage-Image')
print( imgs )
for img in imgs:
img_link = img.get_attribute('src')
get_img(img_link, key, count)
count += 1
print(img_link)
page += 1
driver.close()
driver.quit()
def main():
directory = os.getcwd()
key = 'dogs'
download_img(directory, key)
if __name__ == "__main__":
main()