jeudi 9 janvier 2020

Program skips while

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 dont 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 doesnt even print the "Success!". Anyone has an Idea why it skips the else part?

I am really a beginner, so I'm sorry if this is a dumb question.

Aucun commentaire:

Enregistrer un commentaire