Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'NoneType' object has no attribute 'split'

I have a script with these two functions:

# Getting content of each page
def GetContent(url):
    response = requests.get(url)
    return response.content

# Extracting the sites
def CiteParser(content):
    soup = BeautifulSoup(content)
    print "---> site #: ",len(soup('cite'))
    result = []
    for cite in soup.find_all('cite'):
        result.append(cite.string.split('/')[0])
    return result

When I run program I have the following error:

result.append(cite.string.split('/')[0])
AttributeError: 'NoneType' object has no attribute 'split'

Output Sample:

URL: <URL That I use to search 'can be google, bing, etc'>
---> site #:  10
site1.com
.
.
.
site10.com

URL: <URL That I use to search 'can be google, bing, etc'>
File "python.py", line 49, in CiteParser
    result.append(cite.string.split('/')[0])
AttributeError: 'NoneType' object has no attribute 'split'
like image 572
MLSC Avatar asked Sep 17 '14 05:09

MLSC


People also ask

How do I fix TypeError NoneType object is not Subscriptable in Python?

TypeError: 'NoneType' object is not subscriptable Solution The best way to resolve this issue is by not assigning the sort() method to any variable and leaving the numbers.


2 Answers

It can happen, that the string has nothing inside, than it is "None" type, so what I can suppose is to check first if your string is not "None"

# Extracting the sites
def CiteParser(content):
    soup = BeautifulSoup(content)
    #print soup
    print "---> site #: ",len(soup('cite'))
    result = []
    for cite in soup.find_all('cite'):
        if cite.string is not None:
            result.append(cite.string.split('/'))
            print cite
    return result
like image 191
user1767754 Avatar answered Oct 04 '22 16:10

user1767754


for cite in soup.find_all('cite'):
    if( (cite.string is None) or (len(cite.string) == 0)):
        continue
    result.append(cite.string.split('/')[0])
like image 27
cppcoder Avatar answered Oct 04 '22 15:10

cppcoder