23

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'
6
  • Can you provide example input for the content parameter? Commented Sep 17, 2014 at 5:08
  • 2
    cite.string is returning you a NoneType Commented Sep 17, 2014 at 5:13
  • @cppcoder How can I get rid of this error? Commented Sep 17, 2014 at 5:15
  • Make sure that cite.string has proper values in each iteration. Validate it before applying split function. Commented Sep 17, 2014 at 5:16
  • In order to provide answers, you need to tell us what is cite object and what does cite.string return. Commented Sep 17, 2014 at 5:19

4 Answers 4

19

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
Sign up to request clarification or add additional context in comments.

3 Comments

Yes... I think this is what I needed... let me check... Thanks
For checking NoneType, it is preferable to use is or is not rather than == or !=
Yes i am agreeing with that, it was not so pythonic.
1
for cite in soup.find_all('cite'):
    if( (cite.string is None) or (len(cite.string) == 0)):
        continue
    result.append(cite.string.split('/')[0])

2 Comments

I have also error: TypeError: object of type 'NoneType' has no len().. I think I should remove or (len(cite.string) == 0))... yes?
You could remove that. But len check was intentionally made second to None check so that None objects are not checked for len.
0

I upgraded conda and threadpoolctl. It worked.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
for me upgrading threadpoolctl (pip install threadpoolctl==3.1.0 ) was enough as others have mentioned it in other threads.
0

If you're trying to use stable diffusion amd then probably missing https://download.amd.com/developer/eula/rocm-hub/AMD-Software-PRO-Edition-24.Q3-Win10-Win11-For-HIP.exe That was my case, after installing HIP; I was able to pass the error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.