我使用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下,編碼類型是否會導致這種問題?)。