[Tutor] Finding text in a long string
Timothy M. Brauch
tbrauch@mindless.com
Mon Jun 16 22:55:01 2003
I am just having troubles with something I know is easy.
I've got a small script written that pulls the data from a webpage. What I
am trying to do is to see if "image1.gif" is in the html or not. The
problem I am having is that it might be labelled as
http://domain.com/image1.gif or it might be ../images/image1.gif or
something else (it can change with each page) and it is going to be in an
html tag. I could do it if "image1.gif" was plain text. I'm having trouble
however in this case.
I tried some things like:
data = urllib.urlopen(ADDRESS).read()
if "image1.gif" in data:
print "Success"
else: print "Failure
That gives me the error
Traceback (most recent call last):
File "<pyshell#23>", line 1, in ?
if "offline.gif" in data:
TypeError: 'in <string>' requires character as left operand
which tells me it can only check if a certain character is in a string or
not.
I've tried
dataList = data.split()
for i in dataList:
if "image1.gif" in i
...
I get the same error. I've tried. Because the location of "image1.gif" can
change and it could be either a relative location or an absolute location,
the following doesn't work either:
for i in dataList:
if i == "image1.gif"
I feel like a regular expression would help here, but I'm not sure how I
would do this. Any suggestions?
- Tim