Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to be able to wrap a div based on it's id. For example given the following HTML:

<body>
    <div id="info">
        <div id="a1">
        </div>
        <div id="a2">
            <div id="description">
            </div>
            <div id="links">
                <a href="http://example.com">link</a>
            </div>
        </div>
    </div>
</body>

I want to write a Python function that takes a document, an id, and a selector. and will wrap the given id in the given document in a div with the class or id selector. For example, lets say that the HTML above is in a variable doc

wrap(doc,'#a2','#wrapped')

will return the following HTML:

<body>
    <div id="info">
        <div id="a1">
        </div>
        <div id="wrapped">
            <div id="a2">
                <div id="description">
                </div>
                <div id="links">
                    <a href="http://example.com">link</a>
                </div>
            </div>
        </div>
    </div>
</body>

I looked at some XML parsers and Python HTMLParser, but I have not found anything that gives me the capability to not only get everything inside a specific tag, but then be able to append strings and easily edit the document. If one does not exist, what would be a good approach to this?

share|improve this question
2  
Have you tried BeautifulSoup? – zhangyangyu 6 hours ago
Yeah, BeautifulSoup rocks! – ElmoVanKielmo 6 hours ago
I would rather not use BeautifulSoup for this as it is just for one quick thing in a module I am writing and I don't want to have to import something of that magnitude, and i am not even sure how to go about this in beautifulSoup, but I will check it out if there really is no other way – Ryan Saxe 6 hours ago

2 Answers

I recommend BeautifulSoup though it will bring some dependency but also a lot convenience. The following code can acheieve the goal of the wrap:

    from bs4 import BeautifulSoup
    data = '''<body>
                <div id="info">
                  <div id="a1">
                  </div>
                  <div id="a2">
                    <div id="description">
                    </div>
                    <div id="links">
                      <a href="http://example.com">link</a>
                    </div>
                  </div>
                </div>
              </body>'''
    soup = BeautifulSoup(data)
    div = soup.find('div', attrs={'id': 'a2'})
    div.wrap(soup.new_tag('div', id='wrapper'))

And then print soup.prettify() we can see the result:

<html>
 <body>
  <div id="info">
   <div id="a1">
   </div>
   <div id="wrapper">
    <div id="a2">
     <div id="description">
     </div>
     <div id="links">
      <a href="http://example.com">
       link
      </a>
     </div>
    </div>
   </div>
  </div>
 </body>
</html>
share|improve this answer
from BeautifulSoup import BeautifulSoup

#div1 is to be wrapped with div2
def wrap(doc,div1_id,div2_id)
    pool = BeautifulSoup(doc)
    for div in pool.findAll('div', attrs={'id':div1_id}):
        div.replaceWith('<div id='+div2_id+'>' + div.prettify() + '</div>' )
    return pool.prettify()

wrap(doc,'a2','wrapped')
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.