-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-imgs.py
More file actions
37 lines (27 loc) · 736 Bytes
/
get-imgs.py
File metadata and controls
37 lines (27 loc) · 736 Bytes
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
from bs4 import BeautifulSoup as bs
import os
#website for scraping images
url = 'https://www.pexels.com/search/dogs/'
#download page to parse
page = requests.get(url)
soup = bs(page.text, 'html.parser')
#locate elements w img tag
image_tags = soup.findAll('img')
#check for and/or make new folder to store imgs
if not os.path.exists('dogs'):
os.makedirs('dogs')
#cd
os.chdir('dogs')
x=0
for image in image_tags:
try:
url = image['src']
source = requests.get(url)
if source.status_code == 200:
with open('dog-' + str(x) + '.jpg', 'wb') as f:
f.write(requests.get(url).content)
f.close()
x+=1
except:
pass