-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagescraper.py
40 lines (29 loc) · 987 Bytes
/
imagescraper.py
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
from re import split
from bs4 import BeautifulSoup
import requests
from PIL import Image
from io import BytesIO
import os
def StartSearch():
search = input("Search for: ")
params = {"q":search}
dir_name = search.replace(" ","_").lower()
if not os.path.isdir(dir_name):
os.makedirs(dir_name)
r=requests.get("http://www.bing.com/images/search", params=params)
soup = BeautifulSoup(r.text,"html.parser")
links = soup.findAll("a",{"class":"thumb"})
for item in links:
try:
imj_obj = requests.get(item.attrs["href"])
print("Getting ", item.attrs["href"])
title = item.attrs["href"].split("/")[-1]
try:
img = Image.open(BytesIO(imj_obj.content))
img.save("./"+dir_name+"/"+title, img.format)
except:
print("Could not save image!")
except:
print("Could not request image!")
StartSearch()
StartSearch()