I have a html file where I would like to insert a tag between the
&
tags using python. If I open the file in
append
mode how do I get to the relevant position where the tag is to be inserted?
alecxe
448k114 gold badges1043 silver badges1167 bronze badges
asked Oct 1, 2013 at 18:23
1
Use BeautifulSoup. Here's an example there a meta
tag is inserted right after the title
tag using insert_after[]:
from bs4 import BeautifulSoup as Soup
html = """
Test Page
test
"""
soup = Soup[html]
title = soup.find['title']
meta = soup.new_tag['meta']
meta['content'] = "text/html; charset=UTF-8"
meta['http-equiv'] = "Content-Type"
title.insert_after[meta]
print soup
prints:
Test Page
test
You can also find head
tag and use insert[] with a specified position:
head = soup.find['head']
head.insert[1, meta]
Also see:
- Add parent tags with beautiful soup
- How to append a tag after a link with BeautifulSoup
answered Oct 1, 2013 at 18:37
alecxealecxe
448k114 gold badges1043 silver badges1167 bronze badges
1
You need an xml manipulation library like exposed here Python and HTML Processing, you can't only open the the file and try to append it.
Hope it helps.
answered Oct 1, 2013 at 18:39
Carlos487Carlos487
1,43713 silver badges17 bronze badges
1
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Prerequisites: Beautifulsoup
Beautifulsoup is a Python library used to extract the contents from the webpages. It is used in extracting the contents from HTML and XML structures. To use this library, we need to install it first. Here we are going to append the text to the existing contents of tag. We will do this with the help of the BeautifulSoup library.
Approach
- Import module
- Open HTML file
- Read contents
- Append content to the required tag
- Save changes to the file
Function used:
Append function of the Beautifulsoup module is used to append content to desired tags.
Syntax:
append[“”
File Used:
HTML
Append
Append to the
We are going to append to the contents using
Python Code:
Python3
from
bs4
import
BeautifulSoup
file
=
open
[
"gfg.html"
,
"r"
]
contents
=
file
.read[]
soup
=
BeautifulSoup[contents,
"lxml"
]
print
[
"Current content in title tag is:-"
]
print
[soup.title]
soup.title.append[
"Using BS"
]
print
[
"Content after appending is:-"
]
print
[soup.title]
print
[
"\n"
]
print
[
"Current content in heading h2 tag is:-"
]
print
[soup.h2]
soup.h2.append[
"contents of tag"
]
print
[
"Content after appending is:-"
]
print
[soup.h2]
print
[
"\n"
]
print
[
"Current content in paragraph p tag is:-"
]
print
[soup.p]
soup.p.append[
"BeautifulSoup library"
]
print
[
"Content after appending is:-"
]
print
[soup.p]
print
[
"\n"
]
print
[
"Current content in anchor a tag is:-"
]
print
[soup.a]
soup.a.append[
"Geeks Website"
]
print
[
"Content after appending is:-"
]
print
[soup.a]
savechanges
=
soup.prettify[
"utf-8"
]
with
open
[
"output.html"
,
"wb"
] as
file
:
file
.write[savechanges]
Output:
output.html file