-1
import requests
from bs4 import BeautifulSoup

count = 1

while count != 0:
    def count_words(url, the_word):
        r = requests.get(url, allow_redirects=False)
        soup = BeautifulSoup(r.content, 'lxml')
        words = soup.find(text=lambda text: text and the_word in text)
        print(words)
        return len(words)


    def main():
        url = '<URL>'
        word = '<Word>'
        count = count_words(url, word)
        print('\nUrl: {}\ncontains {} occurrences of word: {}'.format(url, count, word))

    if __name__ == '__main__':
        main()
else:
    print("Success!")

TypeError: object of type 'NoneType' has no len()

I don't really understand why I get this error message, if the website has this specific word it keeps repeating until the word disappears. But when the word is not on the website anymore I get this error message and it doesn't even print the "Success!". Anyone has an idea why it skips the else part?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

The Problem is that if none of the Words are found None is returned by the Function. And you cannot call len() of a None Type. So you had to check first if its None and return 0 if that is the case.

Like:

return 0 if words is None else len(words)

Lastly a quick reminder. If words is None and you return a 0 you end up with an endless loop. Because your count = 1 is a global variable and you define a local variable count in your main function. To overcome this you had to do the following inside your main:

def main():
    ....
    global count
    count = count_words(url, word)

I also would consider to change the code an put the while loop inside the count words function.

Kevin
  • 785
  • 2
  • 10
  • 32