我正在使用 python 编写爬虫,因为我需要解析 html,所以我导入 lxml 但它出现了一个奇怪的错误:
<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 下编码的,编码类型会导致这种问题吗?)。