November 2017
Intermediate to advanced
226 pages
5h 59m
English
In the previous recipe, what will happen if we remove the try catch block in the last step?
patten = '(http)?s?:?(\/\/[^"]*\.(?:png|jpg|jpeg|gif|png|svg))'
for line in open('packtpub.txt'):
for m in re.findall(patten, line):
fileName = basename(urlsplit(m[1])[2])
img = urllib2.urlopen('https:' + m[1]).read()
file = open(fileName, "w")
file.write(img)
file.close() break
The script will fail after a few requests due to the error in the URL format. Some extra characters appeared in the URL and this failed the urllib request.
Read now
Unlock full access