python - html- inserting sibling element containing nested tags
How to insert a sibling element containing nested tags ?
I am trying to use the insert_before function but this seems to work only for single tag.
For example, having
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
I try to insert above each <div class= first_class>
<button class="accordion">
<div class="preface">
<i>Text</i>
</div>
</button>
I try to use below logic, but it doesn't work as expected. The section is not being inserted.
section_code = BeautifulSoup('<button class="accordion"><div class="preface"><i>Text</i></div></button>', 'lxml')
section = section.html.body.contents[0]
titels = soup.find_all("h1")
for title in titels:
title.parent.insert_before(section)
How can this be achieved?
Desired output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>Text</i>
</div>
</button>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
python html beautifulsoup
|
show 3 more comments
How to insert a sibling element containing nested tags ?
I am trying to use the insert_before function but this seems to work only for single tag.
For example, having
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
I try to insert above each <div class= first_class>
<button class="accordion">
<div class="preface">
<i>Text</i>
</div>
</button>
I try to use below logic, but it doesn't work as expected. The section is not being inserted.
section_code = BeautifulSoup('<button class="accordion"><div class="preface"><i>Text</i></div></button>', 'lxml')
section = section.html.body.contents[0]
titels = soup.find_all("h1")
for title in titels:
title.parent.insert_before(section)
How can this be achieved?
Desired output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>Text</i>
</div>
</button>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
python html beautifulsoup
1
please fix typo where your for loop iterates throughtitelssand nottitels
– kpie
Nov 15 '18 at 1:00
i think you need to try to usesection_codeinstead ofsectionin the line of the loop. i mean in the linetitle.parent.insert_before(section)
– stormfield
Nov 15 '18 at 1:04
please refer the documentation
– stormfield
Nov 15 '18 at 1:06
corrected the typo in titles, this typo was not a problem though, the loop is working in my script
– Chris
Nov 15 '18 at 1:06
@stormfield using section_code instead of section doesn't work, it inserts the snippet in the wright place but adds unwanted tags for <html> and <body>, as it handles it as a complete html code
– Chris
Nov 15 '18 at 1:10
|
show 3 more comments
How to insert a sibling element containing nested tags ?
I am trying to use the insert_before function but this seems to work only for single tag.
For example, having
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
I try to insert above each <div class= first_class>
<button class="accordion">
<div class="preface">
<i>Text</i>
</div>
</button>
I try to use below logic, but it doesn't work as expected. The section is not being inserted.
section_code = BeautifulSoup('<button class="accordion"><div class="preface"><i>Text</i></div></button>', 'lxml')
section = section.html.body.contents[0]
titels = soup.find_all("h1")
for title in titels:
title.parent.insert_before(section)
How can this be achieved?
Desired output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>Text</i>
</div>
</button>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
python html beautifulsoup
How to insert a sibling element containing nested tags ?
I am trying to use the insert_before function but this seems to work only for single tag.
For example, having
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
I try to insert above each <div class= first_class>
<button class="accordion">
<div class="preface">
<i>Text</i>
</div>
</button>
I try to use below logic, but it doesn't work as expected. The section is not being inserted.
section_code = BeautifulSoup('<button class="accordion"><div class="preface"><i>Text</i></div></button>', 'lxml')
section = section.html.body.contents[0]
titels = soup.find_all("h1")
for title in titels:
title.parent.insert_before(section)
How can this be achieved?
Desired output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>Text</i>
</div>
</button>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
python html beautifulsoup
python html beautifulsoup
edited Nov 15 '18 at 1:02
Chris
asked Nov 15 '18 at 0:18
ChrisChris
342213
342213
1
please fix typo where your for loop iterates throughtitelssand nottitels
– kpie
Nov 15 '18 at 1:00
i think you need to try to usesection_codeinstead ofsectionin the line of the loop. i mean in the linetitle.parent.insert_before(section)
– stormfield
Nov 15 '18 at 1:04
please refer the documentation
– stormfield
Nov 15 '18 at 1:06
corrected the typo in titles, this typo was not a problem though, the loop is working in my script
– Chris
Nov 15 '18 at 1:06
@stormfield using section_code instead of section doesn't work, it inserts the snippet in the wright place but adds unwanted tags for <html> and <body>, as it handles it as a complete html code
– Chris
Nov 15 '18 at 1:10
|
show 3 more comments
1
please fix typo where your for loop iterates throughtitelssand nottitels
– kpie
Nov 15 '18 at 1:00
i think you need to try to usesection_codeinstead ofsectionin the line of the loop. i mean in the linetitle.parent.insert_before(section)
– stormfield
Nov 15 '18 at 1:04
please refer the documentation
– stormfield
Nov 15 '18 at 1:06
corrected the typo in titles, this typo was not a problem though, the loop is working in my script
– Chris
Nov 15 '18 at 1:06
@stormfield using section_code instead of section doesn't work, it inserts the snippet in the wright place but adds unwanted tags for <html> and <body>, as it handles it as a complete html code
– Chris
Nov 15 '18 at 1:10
1
1
please fix typo where your for loop iterates through
titelss and not titels– kpie
Nov 15 '18 at 1:00
please fix typo where your for loop iterates through
titelss and not titels– kpie
Nov 15 '18 at 1:00
i think you need to try to use
section_code instead of section in the line of the loop. i mean in the line title.parent.insert_before(section)– stormfield
Nov 15 '18 at 1:04
i think you need to try to use
section_code instead of section in the line of the loop. i mean in the line title.parent.insert_before(section)– stormfield
Nov 15 '18 at 1:04
please refer the documentation
– stormfield
Nov 15 '18 at 1:06
please refer the documentation
– stormfield
Nov 15 '18 at 1:06
corrected the typo in titles, this typo was not a problem though, the loop is working in my script
– Chris
Nov 15 '18 at 1:06
corrected the typo in titles, this typo was not a problem though, the loop is working in my script
– Chris
Nov 15 '18 at 1:06
@stormfield using section_code instead of section doesn't work, it inserts the snippet in the wright place but adds unwanted tags for <html> and <body>, as it handles it as a complete html code
– Chris
Nov 15 '18 at 1:10
@stormfield using section_code instead of section doesn't work, it inserts the snippet in the wright place but adds unwanted tags for <html> and <body>, as it handles it as a complete html code
– Chris
Nov 15 '18 at 1:10
|
show 3 more comments
1 Answer
1
active
oldest
votes
You just need to do a bit more. If you insert as str instead of bs4.element.Tag. The string will be html encode
from bs4 import BeautifulSoup
html = """
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
"""
insert = """
<button class="accordion"><div class="preface"><i>Text</i></div></button>
"""
insert_content = BeautifulSoup(insert,"lxml")
soup = BeautifulSoup(html,"lxml")
title = soup.find("div")
title.insert_before(insert_content.find("button"))
print(soup.prettify())
Output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>
Text
</i>
</div>
</button>
<div class="first_class">
<h1 id="Heder1">
Header1
</h1>
</div>
</body>
</html>
@ kcorlidy the logic works on the self contained example, however when I pass the html code to the html variable html=BeautifulSoup(open('C://Folder/Test.html'),'lxml') then I get an error: 'Couldn't find a tree builder with the features you requested: lmxl. Do you need to install a parser library?' I use python 3.5. how need I pass the html code to the html variable to make the script working?
– Chris
Nov 16 '18 at 13:05
1
fd = open('C://Folder/Test.html') html = fd.read() fd.close() soup = BeautifulSoup(html;,"lxml")you can input a fd into BeautifulSoup
– kcorlidy
Nov 16 '18 at 13:09
@ kcorlidy still the same problem. I tried to use the html.parser instead of lxml also, then I get a different error 'Cannot insert None into a tag'
– Chris
Nov 16 '18 at 13:36
1
@Chris i changelxmltohtml.parser, it still works. My env is py35, bs4-4.5.1. and print your content before you insert it, or useif html:to make sure it is notNone
– kcorlidy
Nov 16 '18 at 13:59
@ kcorlidy, worked. there was a problem with passing the argument to a loop. Thanks
– Chris
Nov 16 '18 at 23:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310709%2fpython-html-inserting-sibling-element-containing-nested-tags%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You just need to do a bit more. If you insert as str instead of bs4.element.Tag. The string will be html encode
from bs4 import BeautifulSoup
html = """
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
"""
insert = """
<button class="accordion"><div class="preface"><i>Text</i></div></button>
"""
insert_content = BeautifulSoup(insert,"lxml")
soup = BeautifulSoup(html,"lxml")
title = soup.find("div")
title.insert_before(insert_content.find("button"))
print(soup.prettify())
Output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>
Text
</i>
</div>
</button>
<div class="first_class">
<h1 id="Heder1">
Header1
</h1>
</div>
</body>
</html>
@ kcorlidy the logic works on the self contained example, however when I pass the html code to the html variable html=BeautifulSoup(open('C://Folder/Test.html'),'lxml') then I get an error: 'Couldn't find a tree builder with the features you requested: lmxl. Do you need to install a parser library?' I use python 3.5. how need I pass the html code to the html variable to make the script working?
– Chris
Nov 16 '18 at 13:05
1
fd = open('C://Folder/Test.html') html = fd.read() fd.close() soup = BeautifulSoup(html;,"lxml")you can input a fd into BeautifulSoup
– kcorlidy
Nov 16 '18 at 13:09
@ kcorlidy still the same problem. I tried to use the html.parser instead of lxml also, then I get a different error 'Cannot insert None into a tag'
– Chris
Nov 16 '18 at 13:36
1
@Chris i changelxmltohtml.parser, it still works. My env is py35, bs4-4.5.1. and print your content before you insert it, or useif html:to make sure it is notNone
– kcorlidy
Nov 16 '18 at 13:59
@ kcorlidy, worked. there was a problem with passing the argument to a loop. Thanks
– Chris
Nov 16 '18 at 23:23
add a comment |
You just need to do a bit more. If you insert as str instead of bs4.element.Tag. The string will be html encode
from bs4 import BeautifulSoup
html = """
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
"""
insert = """
<button class="accordion"><div class="preface"><i>Text</i></div></button>
"""
insert_content = BeautifulSoup(insert,"lxml")
soup = BeautifulSoup(html,"lxml")
title = soup.find("div")
title.insert_before(insert_content.find("button"))
print(soup.prettify())
Output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>
Text
</i>
</div>
</button>
<div class="first_class">
<h1 id="Heder1">
Header1
</h1>
</div>
</body>
</html>
@ kcorlidy the logic works on the self contained example, however when I pass the html code to the html variable html=BeautifulSoup(open('C://Folder/Test.html'),'lxml') then I get an error: 'Couldn't find a tree builder with the features you requested: lmxl. Do you need to install a parser library?' I use python 3.5. how need I pass the html code to the html variable to make the script working?
– Chris
Nov 16 '18 at 13:05
1
fd = open('C://Folder/Test.html') html = fd.read() fd.close() soup = BeautifulSoup(html;,"lxml")you can input a fd into BeautifulSoup
– kcorlidy
Nov 16 '18 at 13:09
@ kcorlidy still the same problem. I tried to use the html.parser instead of lxml also, then I get a different error 'Cannot insert None into a tag'
– Chris
Nov 16 '18 at 13:36
1
@Chris i changelxmltohtml.parser, it still works. My env is py35, bs4-4.5.1. and print your content before you insert it, or useif html:to make sure it is notNone
– kcorlidy
Nov 16 '18 at 13:59
@ kcorlidy, worked. there was a problem with passing the argument to a loop. Thanks
– Chris
Nov 16 '18 at 23:23
add a comment |
You just need to do a bit more. If you insert as str instead of bs4.element.Tag. The string will be html encode
from bs4 import BeautifulSoup
html = """
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
"""
insert = """
<button class="accordion"><div class="preface"><i>Text</i></div></button>
"""
insert_content = BeautifulSoup(insert,"lxml")
soup = BeautifulSoup(html,"lxml")
title = soup.find("div")
title.insert_before(insert_content.find("button"))
print(soup.prettify())
Output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>
Text
</i>
</div>
</button>
<div class="first_class">
<h1 id="Heder1">
Header1
</h1>
</div>
</body>
</html>
You just need to do a bit more. If you insert as str instead of bs4.element.Tag. The string will be html encode
from bs4 import BeautifulSoup
html = """
<html>
<body>
<div class= first_class>
<h1 id=Heder1>Header1</h1>
</div>
</body>
</html>
"""
insert = """
<button class="accordion"><div class="preface"><i>Text</i></div></button>
"""
insert_content = BeautifulSoup(insert,"lxml")
soup = BeautifulSoup(html,"lxml")
title = soup.find("div")
title.insert_before(insert_content.find("button"))
print(soup.prettify())
Output
<html>
<body>
<button class="accordion">
<div class="preface">
<i>
Text
</i>
</div>
</button>
<div class="first_class">
<h1 id="Heder1">
Header1
</h1>
</div>
</body>
</html>
edited Nov 15 '18 at 2:53
answered Nov 15 '18 at 2:46
kcorlidykcorlidy
2,2182518
2,2182518
@ kcorlidy the logic works on the self contained example, however when I pass the html code to the html variable html=BeautifulSoup(open('C://Folder/Test.html'),'lxml') then I get an error: 'Couldn't find a tree builder with the features you requested: lmxl. Do you need to install a parser library?' I use python 3.5. how need I pass the html code to the html variable to make the script working?
– Chris
Nov 16 '18 at 13:05
1
fd = open('C://Folder/Test.html') html = fd.read() fd.close() soup = BeautifulSoup(html;,"lxml")you can input a fd into BeautifulSoup
– kcorlidy
Nov 16 '18 at 13:09
@ kcorlidy still the same problem. I tried to use the html.parser instead of lxml also, then I get a different error 'Cannot insert None into a tag'
– Chris
Nov 16 '18 at 13:36
1
@Chris i changelxmltohtml.parser, it still works. My env is py35, bs4-4.5.1. and print your content before you insert it, or useif html:to make sure it is notNone
– kcorlidy
Nov 16 '18 at 13:59
@ kcorlidy, worked. there was a problem with passing the argument to a loop. Thanks
– Chris
Nov 16 '18 at 23:23
add a comment |
@ kcorlidy the logic works on the self contained example, however when I pass the html code to the html variable html=BeautifulSoup(open('C://Folder/Test.html'),'lxml') then I get an error: 'Couldn't find a tree builder with the features you requested: lmxl. Do you need to install a parser library?' I use python 3.5. how need I pass the html code to the html variable to make the script working?
– Chris
Nov 16 '18 at 13:05
1
fd = open('C://Folder/Test.html') html = fd.read() fd.close() soup = BeautifulSoup(html;,"lxml")you can input a fd into BeautifulSoup
– kcorlidy
Nov 16 '18 at 13:09
@ kcorlidy still the same problem. I tried to use the html.parser instead of lxml also, then I get a different error 'Cannot insert None into a tag'
– Chris
Nov 16 '18 at 13:36
1
@Chris i changelxmltohtml.parser, it still works. My env is py35, bs4-4.5.1. and print your content before you insert it, or useif html:to make sure it is notNone
– kcorlidy
Nov 16 '18 at 13:59
@ kcorlidy, worked. there was a problem with passing the argument to a loop. Thanks
– Chris
Nov 16 '18 at 23:23
@ kcorlidy the logic works on the self contained example, however when I pass the html code to the html variable html=BeautifulSoup(open('C://Folder/Test.html'),'lxml') then I get an error: 'Couldn't find a tree builder with the features you requested: lmxl. Do you need to install a parser library?' I use python 3.5. how need I pass the html code to the html variable to make the script working?
– Chris
Nov 16 '18 at 13:05
@ kcorlidy the logic works on the self contained example, however when I pass the html code to the html variable html=BeautifulSoup(open('C://Folder/Test.html'),'lxml') then I get an error: 'Couldn't find a tree builder with the features you requested: lmxl. Do you need to install a parser library?' I use python 3.5. how need I pass the html code to the html variable to make the script working?
– Chris
Nov 16 '18 at 13:05
1
1
fd = open('C://Folder/Test.html') html = fd.read() fd.close() soup = BeautifulSoup(html;,"lxml") you can input a fd into BeautifulSoup– kcorlidy
Nov 16 '18 at 13:09
fd = open('C://Folder/Test.html') html = fd.read() fd.close() soup = BeautifulSoup(html;,"lxml") you can input a fd into BeautifulSoup– kcorlidy
Nov 16 '18 at 13:09
@ kcorlidy still the same problem. I tried to use the html.parser instead of lxml also, then I get a different error 'Cannot insert None into a tag'
– Chris
Nov 16 '18 at 13:36
@ kcorlidy still the same problem. I tried to use the html.parser instead of lxml also, then I get a different error 'Cannot insert None into a tag'
– Chris
Nov 16 '18 at 13:36
1
1
@Chris i change
lxml to html.parser, it still works. My env is py35, bs4-4.5.1. and print your content before you insert it, or use if html: to make sure it is not None– kcorlidy
Nov 16 '18 at 13:59
@Chris i change
lxml to html.parser, it still works. My env is py35, bs4-4.5.1. and print your content before you insert it, or use if html: to make sure it is not None– kcorlidy
Nov 16 '18 at 13:59
@ kcorlidy, worked. there was a problem with passing the argument to a loop. Thanks
– Chris
Nov 16 '18 at 23:23
@ kcorlidy, worked. there was a problem with passing the argument to a loop. Thanks
– Chris
Nov 16 '18 at 23:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310709%2fpython-html-inserting-sibling-element-containing-nested-tags%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
please fix typo where your for loop iterates through
titelssand nottitels– kpie
Nov 15 '18 at 1:00
i think you need to try to use
section_codeinstead ofsectionin the line of the loop. i mean in the linetitle.parent.insert_before(section)– stormfield
Nov 15 '18 at 1:04
please refer the documentation
– stormfield
Nov 15 '18 at 1:06
corrected the typo in titles, this typo was not a problem though, the loop is working in my script
– Chris
Nov 15 '18 at 1:06
@stormfield using section_code instead of section doesn't work, it inserts the snippet in the wright place but adds unwanted tags for <html> and <body>, as it handles it as a complete html code
– Chris
Nov 15 '18 at 1:10