2012-07-11 54 views
0

我使用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']) 

请帮我这两个问题:

  1. 为什么类型为dict但错误说NoneType?我想要匹配“树”中的某些东西,但它不起作用(网站编码在GBK下,编码类型是否会导致这种问题?)。

回答

1
  1. 你得到一个AttributeError,这意味着tree没有xpath属性,因为它已成为None,而不是c没有xpath关键,那将会是一种KeyError代替。

    很明显,我们在这里错过了一些代码,其中tree被设置为“无”。

  2. 您不打印您的tree.xpath()调用的结果,因此您的代码中没有任何内容(与我们共享)打印m。对于我们所知的全部,tree.xpath()调用可能正常工作。

字里行间和猜测一点,你要分配的tree.xpath()结果回tree,和你的XPath表达式没有匹配,返回无。下次进入循环时,您现在有None而不是ElementTreeNode,因此xpath()呼叫失败,并显示AttributeError

0

关于第一个问题,错误是告诉你,tree是没有,因为那是你想读的xpath属性是什么。但是,您正在打印c的类型,而不是tree

我不明白你在问你的第二个问题。