-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.py
More file actions
53 lines (40 loc) · 1.31 KB
/
lab1.py
File metadata and controls
53 lines (40 loc) · 1.31 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
import os #СОЗДАНИЕ ПАПОК
path = 'C:\\Users\\TUFman\\Desktop\\python\\python\\'
projectname = 'dataset'
folders = ['tigers', 'leopardes']
def createfolder(path):
if not os.path.exists(path):
os.mkdir(path)
fullpath = os.path.join(path, projectname)
createfolder(fullpath)
for f in folders:
folder = os.path.join(fullpath, f)
createfolder(folder)
!pip install beautifulsoup4
import time
import requests
from bs4 import BeautifulSoup
url = "https://yandex.ru/images/search?text=tiger&from=tabbar"
r = requests.get(url)
r.text
soup = BeautifulSoup(r.text, 'lxml')
soup
blocks = soup.findAll('div', class_='serp-item__preview')
count = 0
for block in blocks:
if (block):
preview_block = block.find('a', class_='serp-item__link').get('href')
link = (preview_block.split('=')[3])
link2 = link.replace('%3A', ':')
link3 = link2.replace('%2F', '/')
link4 = (link3.split('&')[0])
print(link4)
try:
image_byt = requests.get(f'{link4}').content
time.sleep(1)
with open(f'C:\\Users\\TUFman\\Desktop\\python\\python\\dataset\\tigers\\{count}.jpg', 'wb') as file:
file.write(image_byt)
count += 1
print(count)
except:
continue