html body around with a be...">

How to use BeautifulSoup to pack body contents with a div container

How can I wrap my <div data-role="content"></div>html body around with a beautiful soup?

I tried to start with the following, but could not make any progress:

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(u"%s" % response)
    wrapper = soup.new_tag('div', **{"data-role":"content"})
    soup.body.append(wrapper)
    for content in soup.body.contents:
        wrapper.append(content)

I also tried using body.children, but no luck.

This adds a shell to the body, but does not wrap the body contents as I need

- change -

I came here, but now I get duplicate body elements such as <body><div data-role="content"><body>content here</body></div></body>

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(u"%s" % response)
    wrapper = soup.new_tag('div', **{"data-role":"content"})
    new_body = soup.new_tag('body')
    contents = soup.body.replace_with(new_body)
    wrapper.append(contents)
    new_body.append(wrapper)
+4
source share
2 answers

How about this?

from bs4 import BeautifulSoup
soup = BeautifulSoup(unicode(response))
wrapper = soup.new_tag('div', **{"data-role":"content"})
body_children = list(soup.body.children)
soup.body.clear()
soup.body.append(wrapper)
for child in body_children:
    wrapper.append(child)
+3
source

Great use case for BeautifulSoup wrap():

from bs4 import BeautifulSoup, Tag

response = """
<body>
    <p>test1</p>
    <p>test2</p>
</body>
"""

soup = BeautifulSoup(response, 'html.parser')

wrapper = soup.new_tag('div', **{"data-role": "content"})
soup.body.wrap(wrapper)

print soup.prettify()

prints:

<div data-role="content">
 <body>
  <p>
   test1
  </p>
  <p>
   test2
  </p>
 </body>
</div>

UPD:

from bs4 import BeautifulSoup

response = """<html>
<head>
    <title>test</title>
</head>
<body>
    <p>test</p>
</body>
</html>
"""

soup = BeautifulSoup(response)

wrapper = soup.new_tag('div', **{"data-role": "content"})
soup.body.wrap(wrapper)

print soup.prettify()

gives:

<html>
 <head>
  <title>
   test
  </title>
 </head>
 <div data-role="content">
  <body>
   <p>
    test
   </p>
  </body>
 </div>
</html>
0
source

All Articles