From the site I get data through the variable name_elem_site . Data - 1 word. After some time, it is replaced by another. Maybe even 10 words per second or more. Those. instead of a variable it can be: Word1, Word2, Word3, .., Word2, Word3, .., WordN.

It is necessary to record this data in a list (?) And place it in a log file1. Then, read this log file and count the number of repetitions with output to the console and write to another log file2.

with open("logfile.txt", "w") as log: while True: name_elem_site = text_element_by_class_name(driver, "texttexttext") a = [] for word in name_elem_site: # хз какое условие, количество слов не известно, но явно не больше 50 a.append(str(name_elem_site)) print(a) log.write(str(name_elem_site) + "\n") log.flush() 

Without a counter, such a code, but of course there is no part and it is not correct, because in the log file for 100,500 repetitions of the current word on the site.

    1 answer 1

    In order for the example to be complete, we will first make some non-real repository that will return the words from the list to us.

     def generator(count): words = ["Слово 1", "Слово 2", "Слово 3", "Слово 4", "Слово 5", "Слово 6", "Слово 7", "Слово 8", "Слово 9"] for i in range(count): yield words[random.randrange(len(words))] 

    With reading everything is simple, you just need to keep track of what the word has changed. To do this, it is enough to keep its current value of currentWord and check when it changes.

     currentWord = "" currentList = [] flog1 = open('text.txt', 'w') for word in generator(50): if word == currentWord: continue currentWord = word currentList.append(word) flog1.write("%s\n" % word) flog1.flush() flog1.close() 

    For counting repetitions, it is better to use a dictionary that allows you to store data in the form of a ключ : значение , where the key is a word and a value is the number of its repetitions.

     currentDict = {} for word in open('text.txt', 'r').read().splitlines(): if word in currentDict: currentDict[word] += 1 else: currentDict[word] = 1 

    That's all, it remains only to record it in the second log file.

     flog2 = open('text2.txt', 'w') for word in currentDict.keys(): flog2.write("%s %d\n" % (word, currentDict[word])) print("%s %d" % (word, currentDict[word]))