2

just wondered if somebody could help me tweak this loop:

I am trying to establish if url's exist but the loop is getting stuck when a url doesn't exist (due to the error of trying to find an non-existent url).

I have added in an exception clause but it is throwing an error :(

Any help is much appreciated!

Does_Exist = []
for i in range (0, len(df),1):
   X=[]
   url = df.iloc[i][0] + df.iloc[i][1]
   ret = requests.head(url)  
   except requests.exceptions.ConnectionError 
   if ret.status_code == 200: 
       X = 'Yes' 
       break
   else: 
       X = 'No'
   Does_Exist.append(X)

This is the error message:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.bspretail.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x00000222ABC46470>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)) 
ewldh20
  • 93
  • 10

1 Answers1

1

Try it with a Try/Except structure:

    Does_Exist = []
    for i in range (0, len(df),1):
       X=[]
       try:
          url = df.iloc[i][0] + df.iloc[i][1]
          ret = requests.head(url)  
          X = 'Yes' 
       except:
         X = 'No'
       Does_Exist.append(X)
nacho
  • 5,280
  • 2
  • 25
  • 34