-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_sequential_easy.py
More file actions
50 lines (32 loc) · 1.31 KB
/
scrape_sequential_easy.py
File metadata and controls
50 lines (32 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
#Script downloads images named sequentially between start_number and stop_number
#with numbers inserted between url_base and urL_end
import requests
import os
import shutil
import time
#-------------------------------------------------------------------------------
url_base = "https://www.blahblahblahblahb.blahcom/images/img-" #url to target
url_end = ".jpg" #files will all end in the same extension
start_number = 1
stop_number = 200
save_directory = './saved/'
#-------------------------------------------------------------------------------
def download_image(url, folder):
if not os.path.exists(folder):
os.makedirs(folder)
# Get the response from the URL
response = requests.get(url, stream=True)
# Check if the request was successful
if response.status_code == 200:
# Get the filename from the URL
filename = os.path.join(folder, url.split("/")[-1])
# Open the output file and save the image into it
with open(filename, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
print("Saved: ", filename)
else:
print('Unable to download image:', url)
for i in range (start_number,stop_number+1):
image_url = url_base+str(i)+url_end
download_image(image_url, save_directory)
#time.sleep(0.1) #optional delay