I am trying to get images dowloaded from image-net.org so that I can create a haar cascade classifier. I am following this tutorial https://www.youtube.com/watch?v=z_6fPS5tDNU&list=PLQVvvaa0QuDdttJXlLtAJxJetJcqmqlQq&index=18 but I am using python 2.7 instead of python 3. So in the tutorial he has the line:
urllib.request.urlretrieve(img, pathToImage)
Instead of import urllib.request
I did this import urllib2
So I tried this but it isn't vaild
urllib2.urlretrieve(i, "Negatives/"+str(num)+".jpg")
Thank you in Advance!
The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.
Finally, requests internally uses urllib3 , but it aims for an easier-to-use API. Great answer, now I have another reason to not use requests, and be more confident when using the new urllib .
You just need to import urllib without '2'
import urllib
urllib.urlretrieve(i, "Negatives/"+str(num)+".jpg")
I found that on different build systems, I'd have different version of Python available, totally out of my control.
So I adjusted my script to get urlretrieve
this way:
import sys
print("Python: " + sys.version)
sys.stdout.flush()
import os, re, difflib
# because somewhere along the line they may have deprecated `urlretrieve`
# mentioned in docs for python [3.5.5, 3.6.6, 3.7.0, 3.8.0a0] that:
# `[urlretrieve] might become deprecated at some point in the future.`
def UrlRetrieve(url, fname=None):
if sys.version_info[0] <= 2:
import urllib
return urllib.urlretrieve(url, fname)
elif sys.version_info[0] <= 3:
import urllib.request
return urllib.request.urlretrieve(url, fname)
else:
import shutil
import tempfile
import urllib.request
with urllib.request.urlopen(url) as response:
if fname is None:
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
shutil.copyfileobj(response, tmp_file)
return (tmp_file.name, response.info())
else:
with io.open(fname) as the_file:
shutil.copyfileobj(response, the_file)
return (fname, response.info())
Then, use it this way:
url = "http://...whatever.../bootstrap.zip"
pair = UrlRetrieve(url)
And then, because what I was importing was definitely python 2, I needed to do this in the 3 world:
if sys.version_info[0] >= 3:
import zipfile
zip_ref = zipfile.ZipFile(pair[0], 'r')
zip_ref.extractall(pair[0] + ".d")
zip_ref.close()
import os
os.system("2to3 -w " + pair[0] + ".d")
sys.path.append(pair[0] + ".d")
else:
sys.path.append(pair[0])
from bootstrap_common import *
I now keep these snippets handle for future scripts where I need to use urlretrieve
on both Python 2 and 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With