Python append to html file

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?

Python append to html file

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

Python append to html file

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

  • Read
  • Discuss
  • 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

    <html lang="en">

    <head>

        <meta charset="UTF-8">

        <meta http-equiv="X-UA-Compatible" content="IE=edge">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <title>Append title>

    head>

    <body>

        <h2>Append to the h2>

    <p>We are going to append to the contents using p>

    body>

    html>

    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:

    Python append to html file

    output.html file


    How do you append to HTML in Python?

    One way to do it: with open("/file/name. html", "r") soup = Soup(file) title = soup. find('title') meta = soup. new_tag('meta') meta['content'] = "text/html; charset=UTF-8" meta['http-equiv'] = "Content-Type" title.

    Can Python be added to HTML?

    At the PyconUS 2022, Anaconda unveiled a new framework called PyScript which uses python in HTML code to build applications. You can use python in your HTML code. You don't need to know javascript. PyScript is not just HTML only, it is more powerful, because of the rich and accessible ecosystem of Python libraries.