title: Mismatched Tag Error While Parsing XML?
body: <p>I'm writing this script that downloads an HTML document from <a href="http://example.com/" rel="nofollow">http://example.com/</a> and attempts to parse it as an XML by using:</p>
  
  
  
  <pre><code>with urllib.request.urlopen("http://example.com/") as f:
      tree = xml.etree.ElementTree.parse(f)
  </code></pre>
  
  <p>However, I keep getting a <code>ParseError: mismatched tag</code> error, supposedly at line 1, column 2781, so I donwloaded the file manually (Ctrl+S on my browser) and checked it, but such position indicates a place in the middle of a string, and not even near the EOF, but there were a few lines before the actual 2781nth character so that might've messed up my calculation of the exact position. However, I tried to download and actually write the response to a file to parse it later by:</p>
  
  <pre><code>response = urllib.request.urlopen("http://example.com/")
  f = open("test.html", "wb")
  f.write(response.read())
  f.close()
  html = open("test.html", "r")
  tree = xml.etree.ElementTree.parse(html)
  </code></pre>
  
  <p>And I'm still getting the same <code>mismatched tag</code> error at the same column, but this time I opened the downloaded html and the only stuff near column 2781 is this:</p>
  
  <blockquote>
    <p><code>;&lt;/script&gt;&lt;/head&gt;&lt;body class</code></p>
  </blockquote>
  
  <p>And the exact 2781nth column marks the first "h" in <code>&lt;/head&gt;</code>, so what could be wrong here? am I missing something?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I've been looking more into it and tried to parse the XML using another parser, this time minidom, but I'm still getting the exact same error at the exact same line, what could be the problem here? this also happens even though I've downloaded the file by several different ways (urllib, curl, wget, even Ctrl+Save on the browser) and the result is the same.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>This is what I've tried so far:</p>
  
  <p>This is an example xml I just got from the API doc, and saved it to text.html:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Example page&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;p&gt;Moved to &lt;a href="http://example.org/"&gt;example.org&lt;/a&gt;
          or &lt;a href="http://example.com/"&gt;example.com&lt;/a&gt;.&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And I tried:</p>
  
  <pre><code>with urllib.request.urlopen("text.html") as f:
      tree = xml.etree.ElementTree.parse(f)
  </code></pre>
  
  <p>And it works, then:</p>
  
  <pre><code>with urllib.request.urlopen("text.html") as f:
      tree = xml.etree.ElementTree.fromstring(f.read())
  </code></pre>
  
  <p>And it also works, but:</p>
  
  <pre><code>with urllib.request.urlopen("http://example.com/") as f:
      xml.etree.ElementTree.parse(f)
  </code></pre>
  
  <p>Doesn't, also tried:</p>
  
  <pre><code>with urllib.request.urlopen("http://example.com/") as f:
      xml.etree.ElementTree.fromstring(f.read())
  </code></pre>
  
  <p>And it doesn't work too, what could be the problem? as far as I can tell the document doesn't have mismatching tags, but perhaps it's too large? it's only 95.2 KB.</p>