0

I'm using the requests library in python to grab a webpage's text data, however this should only occur if a particular sentence is found, yet... it still does.

pageresult = requests.get(combo[0], headers=header).text
        
if "We're sorry, but the page you were looking for couldn't be found" or "Page not found" in pageresult:
    print('on page\n')
    print(combo[0])
    text_file = open("PAGERESULT.txt", "w")
    text_file.write(pageresult)
    text_file.close()

For some reason this if statement still executes, even though the conditions are not met.

Neither "We're sorry, but the page you were looking for couldn't be found" or "Page not found" are in the website text data, yet the statement still executes?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Theta
  • 21
  • 5
  • 2
    Hint: `bool("any non-empty string")` is True – Charles Duffy Jun 07 '22 at 13:49
  • ...and `True or "foo" in "bar"` is _also_ True. – Charles Duffy Jun 07 '22 at 13:50
  • @CharlesDuffy Oh damnit i forgot the additional 'in pageresult'. Thanks – Theta Jun 07 '22 at 13:52
  • Also, minor side-note: Learn to use `with` statements for your file operations. `with open("PAGERESULT.txt", "w") as text_file: text_file.write(pageresult)` is no harder to type, avoids the need to explicitly call `close`, and guarantees the file is actually closed even in the event of an exception. – ShadowRanger Jun 07 '22 at 13:53
  • @ShadowRanger Haha i knew that was coming as i made the post, i normally would use a context manager, not sure why i didn't here! – Theta Jun 07 '22 at 13:56

0 Answers0