tags:

views:

110

answers:

2

I'm searching for a string in a website and checking to see if the location of this string is in the expected location. I know the string starts at the 182nd character, and if I print temp it will even tell me that it is 182, however, the if statement says 182 is not 182.

Some code

f = urllib.urlopen(link)

 #store page contents in 's'
 s = f.read()
 f.close()
 temp = s.find('lettersandnumbers')

 if (htmlsize == "197"):
  #if ((s.find('lettersandnumbers')) == "182"):
  if (temp=="182"):
   print "Glorious"
   doStuff()
  else:
   print "HTML not correct.  Aborting."
 else:
  print htmlsize
  print "File size is incorrect.  Aborting."
+3  A: 

Im not a python guru, but ill take a shot

Try it like this

if (temp == 182)

Why? See SilentGhost answer. It involves types

Tom
You can also use type(temp) to see what type it is. It should return <type 'str'> if it is '182' and <type 'int'> if it is 182. Alternatively you could use if(str(temp) == '182') to make sure it compares the values as a string.
Greg Bray
@Greg: while you *can* do that, why would you want to do it? return value of `str.find` is well defined and understood, no need to introduce more complexity
SilentGhost
@Greg, But doing all of that would show that you don't know what's going on.
Mike Graham
+4  A: 

str.find returns integer, not string. String-integers comparison always returns False.

SilentGhost