Aco*_*orn 6 python lxml html-parsing iterparse
我目前正在尝试迭代解析一个非常大的HTML文档(我知道.. yuck)以减少使用的内存量.我遇到的问题是我遇到了XML语法错误,例如:
lxml.etree.XMLSyntaxError: Attribute name redefined, line 134, column 59
这会导致一切停止.
有没有办法迭代解析HTML而不会窒息语法错误?
目前我正在从XML语法错误异常中提取行号,从文档中删除该行,然后重新启动该过程.看起来像一个非常恶心的解决方案.有没有更好的办法?
编辑:
这就是我目前正在做的事情:
context = etree.iterparse(tfile, events=('start', 'end'), html=True)
in_table = False
header_row = True
while context:
try:
event, el = context.next()
# do something
# remove old elements
while el.getprevious() is not None:
del el.getparent()[0]
except etree.XMLSyntaxError, e:
print e.msg
lineno = int(re.search(r'line (\d+),', e.msg).group(1))
remove_line(tfilename, lineno)
tfile = open(tfilename)
context = etree.iterparse(tfile, events=('start', 'end'), html=True)
except KeyError:
print 'oops keyerror'
Run Code Online (Sandbox Code Playgroud)
完美的解决方案最终成为了Python自己的HTMLParser [docs].
这是我最终使用的(非常糟糕的)代码:
class MyParser(HTMLParser):
def __init__(self):
self.finished = False
self.in_table = False
self.in_row = False
self.in_cell = False
self.current_row = []
self.current_cell = ''
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if not self.in_table:
if tag == 'table':
if ('id' in attrs) and (attrs['id'] == 'dgResult'):
self.in_table = True
else:
if tag == 'tr':
self.in_row = True
elif tag == 'td':
self.in_cell = True
elif (tag == 'a') and (len(self.current_row) == 7):
url = attrs['href']
self.current_cell = url
def handle_endtag(self, tag):
if tag == 'tr':
if self.in_table:
if self.in_row:
self.in_row = False
print self.current_row
self.current_row = []
elif tag == 'td':
if self.in_table:
if self.in_cell:
self.in_cell = False
self.current_row.append(self.current_cell.strip())
self.current_cell = ''
elif (tag == 'table') and self.in_table:
self.finished = True
def handle_data(self, data):
if not len(self.current_row) == 7:
if self.in_cell:
self.current_cell += data
Run Code Online (Sandbox Code Playgroud)
使用该代码,我可以这样做:
parser = MyParser()
for line in myfile:
parser.feed(line)
Run Code Online (Sandbox Code Playgroud)
目前,lxml etree.iterparse 支持关键字参数 restore = True,因此您无需编写HTMLParser的自定义子类来修复损坏的html,您只需将此参数传递给iterparse。
要正确地解析巨大且破碎的html,您只需执行以下操作:
etree.iterparse(tfile, events=('start', 'end'), html=True, recover=True)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2707 次 |
| 最近记录: |