遇到错误“ xml.parsers.expat.ExpatError:标记不匹配”时,如何解析网址?

问题描述

我要提取网页中元素DOCUMENT中的所有链接

target to be parsed

import urllib.request
url = 'https://www.sec.gov/Archives/edgar/data/1326801/000132680120000013/0001326801-20-000013-index-headers.html'
ob=urllib.request.urlopen(url).read()
from xml.dom import minidom
xmldoc = minidom.parseString(ob)

遇到问题:

Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  File "/usr/lib/python3.5/xml/dom/minidom.py",line 1968,in parseString
    return expatbuilder.parseString(string)
  File "/usr/lib/python3.5/xml/dom/expatbuilder.py",line 925,in parseString
    return builder.parseString(string)
  File "/usr/lib/python3.5/xml/dom/expatbuilder.py",line 223,in parseString
    parser.Parse(string,True)
xml.parsers.expat.ExpatError: mismatched tag: line 876,column 23

也许这是格式错误的xml文件,如何以最小化方式加载它?

解决方法

我不知道这个文件是什么,但是它不是XML,因此无法使用XML解析器进行解析。

,

是的,它不是xml文件,请使用lxml.html解析它,并选择所有带有xpath的网址。

import urllib.request
url = 'https://www.sec.gov/Archives/edgar/data/1326801/000132680120000013/0001326801-20-000013-index-headers.html'
ob=urllib.request.urlopen(url).read()
doc = lxml.html.fromstring(ob)
links = doc.xpath('//pre/a')
for link in links:
    print(link.attrib['href'])