Skip to content

Commit

Permalink
解决 'NoneType' object has no attribute 'xpath'
Browse files Browse the repository at this point in the history
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'
```
  • Loading branch information
zangwill authored May 17, 2024
1 parent 287e93f commit e94535b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions weibo_spider/parser/page_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e94535b

Please sign in to comment.