-2

I am using strip() to avoid white spaces,\n, and \t in my scraper. It works fine with selenium but sometimes with scrapy I get 'AttributeError':''Nonetype' obeject has no attribute 'strip'

Renuka
  • 7
  • 5

1 Answers1

2

You get None instead of sting and you try to use None.strip() which doesn't exist - so using any other function can't help you.

You have to use if/else to check what get:

if result is not None: 
    result = result.strip() 

or shorter

if result: 
    result = result.strip() 

EDIT:

You can write it also in one line

result = result.strip() if result else result

but it seems less readable

Eventually you could use try/except

try:
    result = result.strip()
except AttributeError as ex:
    print('ex:', ex)

but it seems even less readable


Of course you can put this code in some function

def my_strip(value):
    if result: 
        return result.strip()
    return value

and then use

result = my_strip(result)
    
furas
  • 134,197
  • 12
  • 106
  • 148