我有一個我正在嘗試解析的html頁面。這是我在用lxml做的事情:lxml解析中的命名空間參數
node=etree.fromstring(html)
>>> node
<Element {http://www.w3.org/1999/xhtml}html at 0x110676a70>
>>> node.xpath('//body')
[]
>>> node.xpath('body')
[]
不幸的是,我所有的xpath調用現在都返回一個空列表。爲什麼會發生這種情況,我如何解決這個問題?
它可能是所有的標籤命名空間,因爲你已經猜到了,可能是最簡單的使用HTML解析模塊http://lxml.de/lxmlhtml.html#parsing-html – Anentropic 2015-02-08 20:57:49
否則與命名空間,你會有可以這樣做:'node.xpath('// html:body',namespaces = {'html':'http://www.w3.org/1999/xhtml'})' – Anentropic 2015-02-08 20:58:39