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?