Mathew
Mathew

Reputation: 1450

Can't parse html using xml.etree.ElementTree

I am trying to parse the xml of google.com however I am getting a 'not well-formed' error. Why is this? Thanks

➜  testing cat code.py
from urllib.request import urlopen; from xml.etree.ElementTree import fromstring
fromstring(urlopen('https://www.google.com').read().replace(b'<!doctype html>',b'<!DOCTYPE html>'))
➜  testing python3 code.py
Traceback (most recent call last):
  File "code.py", line 2, in <module>
    fromstring(urlopen('https://www.google.com').read().replace(b'<!doctype html>',b'<!DOCTYPE html>'))
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/etree/ElementTree.py", line 1315, in XML
    parser.feed(text)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1826
➜  testing

Upvotes: 2

Views: 1555

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24928

You are probably getting the error message because you are trying to parse HTML with an XML parser; it won't work. Try it with a library with an HTML parser. Also, I would recommend getting the page with requests, instead. So together:

import requests
import lxml.html as lh

req = requests.get('https://www.google.com')
lh.fromstring(req.text)

and it should work.

Upvotes: 3

Related Questions