简体   繁体   中英

Python: traceback when using the lxml

code:

response = urllib2.urlopen('xxxxxxwebsite address')
html = response.read()
tree = etree.fromstring(html)
nodes = tree.xpath("//span[contains(@class,'badge-info')")
for node in nodes:
    print(node.text)

Error:

Traceback (most recent call last):
  File "extract1.py", line 11, in <module>
    tree = etree.fromstring(html)
  File "src\lxml\etree.pyx", line 3222, in lxml.etree.fromstring
  File "src\lxml\parser.pxi", line 1877, in lxml.etree._parseMemoryDocument
  File "src\lxml\parser.pxi", line 1765, in lxml.etree._parseDoc
  File "src\lxml\parser.pxi", line 1127, in lxml.etree._BaseParser._parseDoc
  File "src\lxml\parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
  File "src\lxml\parser.pxi", line 711, in lxml.etree._handleParseResult
  File "src\lxml\parser.pxi", line 640, in lxml.etree._raiseParseError
  File "<string>", line 42
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 24 and head, line 42, column 8

Plesae suggest me if there is a better way than lxml to read.

You have a typo:

tree.xpath("//span[contains(@class,'badge-info')]")
#                                               ^ unclosed

The HTML isn't well-formed XML so you'll either have to use HTMLParser() or HTML() so that lxml can recover.

See " Parsing HTML " for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM