From e94535bc31213b6cccdb5ff8e8a7963eb4e6d391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=B5=E7=BA=AC?= <19372775+zangwill@users.noreply.github.com> Date: Fri, 17 May 2024 11:13:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=20'NoneType'=20object=20has?= =?UTF-8?q?=20no=20attribute=20'xpath'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handle_html 函数返回 Element 或者 None,`page_parser.py` 没有处理 None 的情况 ```py 'NoneType' object has no attribute 'xpath' Traceback (most recent call last): File "C:\Users\zw\miniconda3\lib\site-packages\weibo_spider\spider.py", line 178, in get_weibo_info weibos, self.weibo_id_list, to_continue = PageParser( File "C:\Users\zw\miniconda3\lib\site-packages\weibo_spider\parser\page_parser.py", line 47, in __init__ info = self.selector.xpath("//div[@class='c']") AttributeError: 'NoneType' object has no attribute 'xpath' ``` --- weibo_spider/parser/page_parser.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/weibo_spider/parser/page_parser.py b/weibo_spider/parser/page_parser.py index fcd1714..6e35644 100644 --- a/weibo_spider/parser/page_parser.py +++ b/weibo_spider/parser/page_parser.py @@ -44,10 +44,11 @@ def __init__(self, cookie, user_config, page, filter): is_exist = '' for i in range(3): self.selector = handle_html(self.cookie, self.url) - info = self.selector.xpath("//div[@class='c']") - if info is None or len(info) == 0: - continue - is_exist = info[0].xpath("div/span[@class='ctt']") + if self.selector: + info = self.selector.xpath("//div[@class='c']") + if info is None or len(info) == 0: + continue + is_exist = info[0].xpath("div/span[@class='ctt']") if is_exist: PageParser.empty_count = 0 break