Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1002231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:48:07+00:00 2026-05-16T07:48:07+00:00

I am using BeautifulSoup in Python and am having trouble replacing some tags. I

  • 0

I am using BeautifulSoup in Python and am having trouble replacing some tags. I am finding <div> tags and checking for children. If those children do not have children (are a text node of NODE_TYPE = 3), I am copying them to be a <p>.

from BeautifulSoup import Tag, BeautifulSoup

class bar:

 self.soup = BeautifulSoup(self.input)
 foo()
 def foo(self):    
  elements = soup.findAll(True)

  for node in elements:

    # ....other stuff here if not <div> tags.

    if node.name.lower() == "div":
      if not node.find('a'):
        newTag = Tag(self.soup, "p")
        newTag.setString(node.text)
        node.replaceWith(newTag)
        nodesToScore.append(newTag)
      else:
        for n in node.findAll(True):
          if n.getString():  # False if has children
            newTag = Tag(self.soup, "p")
            newTag.setString(n.text)
            n.replaceWith(newTag)

I’m getting an AttributeError:

  File "file.py", line 125, in function
    node.replaceWith(newTag)
  File "BeautifulSoup.py", line 131, in replaceWith
    myIndex = self.parent.index(self)
AttributeError: 'NoneType' object has no attribute 'index'

I do the same replacing on node higher up in the for loop and it works correctly. I’m assuming it’s having problems because of the additional iterating through node as n.

What am I doing wrong or what would be a better way to do this? Thanks!
PS. I’m using Python 2.5 for Google Appengine and BeautifulSoup 3.0.8.1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:48:07+00:00Added an answer on May 16, 2026 at 7:48 am

    The error says:

        myIndex = self.parent.index(self)
    AttributeError: 'NoneType' object has no attribute 'index'
    

    This code occurs on line 131 of BeautifulSoup.py.
    It says that self.parent is None.

    Looking at the surrounding code shows that self should equal node in your code, since node is calling its replaceWith method.(Note: The error message says node.replaceWith, but the code you posted shows n.replaceWith. The code you posted does not correspond to the error message/traceback.) So apparently node.parent is None.

    You could probably avoid the error by placing

    if node.parent is not None:
    

    at some point in the code before node.replaceWith is called.

    Edit: I suggest you use print statements to investigate where in the HTML you are when node.parent is None (i.e. where the error is occurring). Maybe use print node.contents or print node.previous.contents or print node.next.contents to see where you are. Once you see the HTML it might become obvious what pathological situation you are in which is causing node.parent to be None.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that fetches some text from a page using BeautifulSoup soup=
I am using beautifulsoup, and I am getting some htmlparser errors with start tags
I have a webpage that I read using Python and BeautifulSoup, say soup=BeautifulSoup(urllib2.urlopen(site)) .
Here is the my python code using BeautifulSoup. The main issue is with the
I am using Python and BeautifulSoup to extract financial statement information from cities financial
I'm using python and beautifulsoup (new to both!), and I want to login to
I'm using beautiful soup (in Python). I have such hidden input object: <input type=hidden
I am using Beautiful Soup 3.2 on python 2.7.1 here. I have recently been
(The following is using Python 2.6.1) I have 2 strings: >>> a = u'\u05e8\u05db\u05e1'
I'm trying to scrape some data off of the FEC.gov website using python for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.