forked from nateberman/Python-WebImageScraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_scraper.py
More file actions
55 lines (40 loc) · 1.69 KB
/
Copy pathimage_scraper.py
File metadata and controls
55 lines (40 loc) · 1.69 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
#Added support to filter by class name of html element
#Added destination folder support
#Updated code to work with urllib.request
from bs4 import BeautifulSoup
from urllib.request import urlopen
import urllib.request
def make_soup(url):
html = urlopen(url).read()
return BeautifulSoup(html, "html.parser")
def get_images(url):
soup = make_soup(url)
#this makes a list of bs4 element tags
images = [img for img in soup.findAll("type of html element", {"class":"name of the class"})]
print (str(len(images)) + "images found")
print ('Downloading images to directory')
#compile our unicode list of image links
image_links = [each.get('src') for each in images]
for each in image_links:
filename=each.split('/')[-1]
urllib.request.urlretrieve(each, filename)
return image_links
#Warning!! If your Path contains "\U" such as \Users it will not work properly
#To solve this you must double all the \ so that it looks like \\Users
#Do not put this .py file in a location(folder) where are other images with the format you choose
#The program will automatically transfer all the images on the folder of its location to the folder you chose
#Use with caution
entry = input("Paste the site URL:")
get_images(entry)
import os
import shutil
sourcepath="The path where this .py is located at"
source = os.listdir(sourcepath)
folder = input("Paste your desired custom path:")
destinationpath = folder
for files in source:
if files.endswith('.jpg,.png,.gif'):
shutil.move(os.path.join(sourcepath,files), os.path.join(destinationpath,files))
k=input("Finished! Press any key to exit.")
#a standard call looks like this
#get_images('http://www.wookmark.com')