奇怪的蟒蛇错误使用LXML和XPath
问题描述:
我使用Python写一个履带式的,因为我需要解析HTML,所以我导入LXML但它出来一个奇怪的错误时:奇怪的蟒蛇错误使用LXML和XPath
<type 'dict'>
{'xpath': '//ul[@id="i-detail"]/li[1]', 'name': u'\u6807\u9898'}
<type 'dict'>
{'xpath': '//ul[@id="i-detail"]/li[1]', 'name': u'\u6807\u9898'}
<type 'dict'>
{'xpath': '//ul[@id="i-detail"]/li[1]', 'name': u'\u6807\u9898'}
Exception in thread Thread-3:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 477, in run
self.__target(*self.__args, **self.__kwargs)
File "fetcher.py", line 78, in run
self.extractContent(html)
File "fetcher.py", line 151, in extractContent
m = tree.xpath(c['xpath'])
AttributeError: 'NoneType' object has no attribute 'xpath'
<type 'dict'>
{'xpath': '//ul[@id="i-detail"]/li[1]', 'name': u'\u6807\u9898'}
这里是一片我的代码:
for c in self.contents:
print type(c)
print c
m = tree.xpath(c['xpath'])
请帮我这两个问题:
为什么类型为
dict
但错误说NoneType?我想要匹配“树”中的某些东西,但它不起作用(网站编码在GBK下,编码类型是否会导致这种问题?)。
答
-
你得到一个
AttributeError
,这意味着tree
没有xpath
属性,因为它已成为None
,而不是c
没有xpath
关键,那将会是一种KeyError
代替。很明显,我们在这里错过了一些代码,其中
tree
被设置为“无”。 您不打印您的
tree.xpath()
调用的结果,因此您的代码中没有任何内容(与我们共享)打印m
。对于我们所知的全部,tree.xpath()
调用可能正常工作。
字里行间和猜测一点,你要分配的tree.xpath()
结果回tree
,和你的XPath表达式没有匹配,返回无。下次进入循环时,您现在有None
而不是ElementTreeNode
,因此xpath()
呼叫失败,并显示AttributeError
。
答
关于第一个问题,错误是告诉你,tree
是没有,因为那是你想读的xpath
属性是什么。但是,您正在打印c
的类型,而不是tree
。
我不明白你在问你的第二个问题。