find function returns true when false

Question:

I am loading the source code of a web page as a string and then want to search that string for a specific phrase.

This is what I have so far:

from urllib import request
r = request.urlopen("http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?    siteCategoryId=3&T1ID=26&T2ID=35")
bytecode = r.read()
htmlstr = bytecode.decode() #htmlstr is now source code for page

if htmlstr.find("Todays Incidents of Interest (0)")
    print("no incidents today")

I want to see if there have been any incidents today – if there haven’t, there is no need to continue. I have waited until I know the number of incidents is greater than 0 (by going to the site and manually monitoring) but I still get it found; what am I doing wrong?

Or is there a way of grabbing that phrase and seeing what the number is in the brackets? This might be useful, as I will set up a loop later based on that number.

Asked By: Raif Jackson

||

Answers:

string.find() returns -1 on failure, not false.

Answered By: Bill Lynch
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.