mercredi 2 mai 2018

Strange behaviour of Python code

I am scraping name of cities from a site. If a name of a city, which is scraped from a site, is not found in the database, then only that name should be inserted in the city_not_found table in the database. The code is like this:

isFound=0
      for row in data :
         found = re.search(row[0], venue)
         found1 = re.search(row[1], venue)
         #if((found!=None)and(found1!=None)):
         if found != None:
           city = row[0]
           state = row[1]
           country = row[2]
           isFound=1
           break
      if(isFound==0):
         for row in data2:
            found2 = re.search(row[3], venue)
            found3 = re.search(row[1], venue)
            #if((found2!=None)and(found3!=None)):
            if found2 != None:
                city = row[0]
                state = row[1]
                country = row[2]
                break
            else:
                city = 'city_not_found'
                state = ''
                country = ''
                sql = "INSERT IGNORE INTO city_not_found (full_address, page_url) VALUES (%s, %s)"
                cursor.execute(sql, (venue, source_url)) 

It seems that if if found2 != None: condition block executes, the name of the city is inserted in the city_not_found table, whereas, insertion should occur only if the last else block executes.

Aucun commentaire:

Enregistrer un commentaire