Python:为什么下面的xpath返回空列表?
问题描述:
我想从instapaper.com
中提取一些文本和链接。所以我用下面的代码来完成这项工作:Python:为什么下面的xpath返回空列表?
>>> import lxml.html as lh
>>> doc = lh.parse("http://www.instapaper.com/u/folder/1227370/programming")
>>> text = doc.xpath(".//*[@id='bookmark_list']/*/div[3]/a/text()")
>>> len(text)
0
>>> text
[]
正如你可以看到它返回一个空列表,这意味着它无法找到匹配以上的XPath任何文本。
现在,当我在萤火虫/ firepath中使用上述xpath expr
它工作正常。
你可以把它显示40 matching nodes
上面的图片中看到。
所以,我的问题是为什么上面的xpath表达式不适用于python/lxml。
答
没有与ID bookmark_list
没有元素。也许你必须登录
编辑
解析真正 HTML它的工作原理:
doc = lh.parse("http://pastebin.com/raw.php?i=1WpFAfCt")
text = doc.xpath("//*[@id='bookmark_list']/*/div[3]/a/text()")
len(text) # => 40
+0
好的。是的,我已登录。 – ronnie 2012-08-06 10:21:45
请张贴的XML。 – 2012-08-06 10:11:59
尝试删除第一个句点字符。 – 2012-08-06 10:13:02