我需要匹配所有这些开始标记:
<p>
<a href="foo">
Run Code Online (Sandbox Code Playgroud)
但不是这些:
<br />
<hr class="foo" />
Run Code Online (Sandbox Code Playgroud)
我想出了这个,并希望确保我做对了.我只抓住了a-z.
<([a-z]+) *[^/]*?>
Run Code Online (Sandbox Code Playgroud)
我相信它说:
/,然后我有这个权利吗?更重要的是,你怎么看?
我需要使用lxml下载并解析网页并构建UTF-8 xml输出.我认为伪代码模式更具说明性:
from lxml import etree
webfile = urllib2.urlopen(url)
root = etree.parse(webfile.read(), parser=etree.HTMLParser(recover=True))
txt = my_process_text(etree.tostring(root.xpath('/html/body'), encoding=utf8))
output = etree.Element("out")
output.text = txt
outputfile.write(etree.tostring(output, encoding=utf8))
Run Code Online (Sandbox Code Playgroud)
所以webfile可以是任何编码(lxml应该处理这个).Outputfile必须是utf-8.我不知道在哪里使用编码/编码.这个架构好吗?(我找不到关于lxml和编码的好教程,但我可以发现很多问题...)我需要强大的解决方案.
编辑:
因此,对于发送utf-8到lxml,我使用
converted = UnicodeDammit(webfile, isHTML=True)
if not converted.unicode:
print "ERR. UnicodeDammit failed to detect encoding, tried [%s]", \
', '.join(converted.triedEncodings)
continue
webfile = converted.unicode.encode('utf-8')
Run Code Online (Sandbox Code Playgroud)