Counting Characters from an HTML File with Python I just completed level 2 of The Python Challenge on pythonchallenge.com and I am in the process of learning python so please bear with me and any silly mistakes I may have made.
I am looking for some feedback about what I could have done better in my code. Two areas specifically:

How could I have more easily identified the comment section of the HTML file? I used a beat-around-the-bush method that kind of found the end of the comment (or the beginning technically but it is counting from the end) and gave me some extra characters that I was able to recognize and anticipated (the extra &quot;--&gt;&quot; and &quot;-&quot;). What condition would have better found this comment so I could put it in a new string to be counted?

This is what I wrote:
from collections import Counter
import requests

page = requests.get('http://www.pythonchallenge.com/pc/def/ocr.html')

pagetext = &quot;&quot;
pagetext = (page.text)
#find out what number we are going back to

i = 1
x = 4
testchar = &quot;&quot;
testcharstring = &quot;&quot;


while x == 4:
    testcharstring = pagetext[-i:]
    testchar = testcharstring[0]
    if testchar == &quot;-&quot;:
        testcharstring = pagetext[-(i+1)]
        testchar = testcharstring[0]
        if testchar == &quot;-&quot;:
            testcharstring = pagetext[-(i+2)]
            testchar = testcharstring[0]
            if testchar == &quot;!&quot;:
                testcharstring = pagetext[-(i+3)]
                testchar = testcharstring[0]
                if testchar == &quot;&lt;&quot;:
                    x = 3
            else:
                i += 1
                x = 4
        else:
            i += 1
            x = 4
    else:
        i += 1
print(i)

newstring = pagetext[-i:]

charcount = Counter(newstring)

print(charcount)

And this is the source HTML:
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;ocr&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;../style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;&lt;img src=&quot;ocr.jpg&quot;&gt;
&lt;br&gt;&lt;font color=&quot;#c03000&quot;&gt;
recognize the characters. maybe they are in the book, &lt;br&gt;but MAYBE they 
are in the page source.&lt;/center&gt;

&lt;br&gt;
&lt;br&gt;
&lt;br&gt;

&lt;font size=&quot;-1&quot; color=&quot;gold&quot;&gt;
General tips:
&lt;li&gt;Use the hints. They are helpful, most of the times.&lt;/li&gt;
&lt;li&gt;Investigate the data given to you.&lt;/li&gt;
&lt;li&gt;Avoid looking for spoilers.&lt;/li&gt;
&lt;br&gt;
Forums: &lt;a href=&quot;http://www.pythonchallenge.com/forums&quot;/&gt;Python Challenge Forums&lt;/a&gt;, 
read before you post.
&lt;br&gt;
IRC: irc.freenode.net #pythonchallenge
&lt;br&gt;&lt;br&gt;
To see the solutions to the previous level, replace pc with pcc, i.e. go 
to: http://www.pythonchallenge.com/pcc/def/ocr.html

&lt;/body&gt;
&lt;/html&gt;

&lt;!--
find rare characters in the mess below:
--&gt;

&lt;!--

Followed by thousands of characters and the comment concludes with '--&gt;'
I don’t have enough reputation to comment, so I must say this in an answer.
It looks clunky to use
    while x == 4:

and then do
    x = 3

whenever you want to break out of the loop.
It looks better to do
    while True:

and when you want to break out of the loop do
    break

Cheers!