Keeping just the rare characters in a long string
x is not a good variable name.  I suggest page_source.

Your approach is inefficient because you re-count all of the occurrences of each character, which is O(n2), where n is the length of the string.

It would be better to use a Counter.  It's more efficient because counts all characters using just one pass through the string.  (We call that O(n).)

from collections import Counter

count = Counter(text)
s = ''.join(char for char in text if count[char] &lt; 10)
print(s)


If you wanted to make your own counter with similar performance characteristics, you could do something similar to this:

count = {}
for char in text:
    count[char] = 1 + count.get(char, 0)