I have this code here working for me:
import urllib3
http = urllib3.PoolManager()
location = 'Drive\\where-images-go\\'
N = 0
for x in range(0, 5000):
urls = ('http://00000.IMAGE_1_'+str(x)+'.jpg')
r = http.request('GET', urls)
Name = str(N+1)
N += 1
with open(location + 'image'+Name+'_image.jpg', 'wb') as img:
img.write(r.data)
This code will create a url that will count the str(x) from 0 to 5000. But I would also like to add another counter that will count by 1 for every 5000 the above counter goes up where those five zeros are. for example:
import urllib3
http = urllib3.PoolManager()
location = 'Drive\\where-images-go\\'
N = 0
for x in range(0, 224999):
for every 5000 in x:
othercounter = oc
oc = oc +1
urls = ('http://'+str(oc)+'.IMAGE_1_'+str(x)+'.jpg')
r = http.request('GET', urls)
Name = str(N+1)
N += 1
with open(location + 'image'+Name+'_image.jpg', 'wb') as img:
img.write(r.data)
So every time str(x) hits 5000 the oc(othercounter) will go up 1. As the range of the 2nd example code is (0,224999) the oc counter will be 44 and the last url will be 'http://00044.IMAGE_1_224999.jpg'. The very first image will have been 'http://00000.IMAGE_1_1.jpg'.
How to solve this problem?
you can use mod %
to print every 5000, and I have pointed out other things you should do to achieve your goal:
oc
should be initialized as -1
, because you want to start from 0
oc
keep 5 width with prefix 0
, like 00044
, so you should use rjust
here: str(oc).rjust(5, '0')
0
is not necessary in range(0, 224999)
import urllib3
http = urllib3.PoolManager()
location = 'Drive\\where-images-go\\'
N = 0
oc = -1
for x in range(224999):
N += 1
if x % 5000 == 0:
oc += 1
urls = ('http://' + str(oc).rjust(5, '0') + '.IMAGE_1_' + str(N) + '.jpg')
r = http.request('GET', urls)
with open(location + 'image'+str(N)+'_image.jpg', 'wb') as img:
img.write(r.data)
# test code, need to remove
# the first
if x == 0:
print('first url: {}'.format(urls))
# the last
if x == 224998:
print('last url: {}'.format(urls))
output
first url: http://00000.IMAGE_1_1.jpg
last url: http://00044.IMAGE_1_224999.jpg
Hope that helps you, and comment if you have further questions. : )