I am ready to run this code but before I want to fix the exception handling:
for l in bios:
OpenThisLink = url + l
try:
response = urllib2.urlopen(OpenThisLink)
except urllib2.HTTPError:
pass
bio = response.read()
item = re.search('(JD)(.*?)(\d+)', bio)
....
As suggested here, I added the try...except but now if a page doesn’t open I get this error:
bio = response.read()
NameError: name 'response' is not defined
So the program continues to execute. Instead I want it to go back to the for loop and try the next url. I tried break instead of pass but that ends the program. Any suggestions?
Use
continueinstead ofbreak.The statement
passis a no-op (meaning that it doesn’t do anything). The program just continues to the next statement, which is why you get an error.breakexits the loops and continues running from the next statement immediately after the loop. In this case, there are no more statements, which is why your program terminates.continuerestarts the loop but with the next item. This is exactly what you want.