Yogesh
Yogesh

Reputation: 386

Append an attribute to input tag using BeautifulSoup

I have a simple HTML file that contains some input fields and other tags. My goal is to get all the input tags and append a new attribute value to each of them. It is to be noted that the value of this new attribute will be decided/populated from an array of strings.

I'm using the BeautifulSoup package of Python to parse this HTML file, and then append this new attribute to each of the input tags.

This my HTML file which I'm parsing:

test.html

<!DOCTYPE html>
<html>
<head>
    <title>Testing Accessibility</title>
</head>
<body>

    <h1>Hello World</h1>

    <div>
        <h2>This is a title</h2>
        <p>Welcome, to the world's first <strong>Accessibility Script</strong>. You will be amazed by it completely!<br>Hope you'll enjoy it!</p>
    </div>
    <hr>
    <div>
        <form>
            <label>First Name</label>
            <input type="text" name="fname" placeholder="First Name">
            <label>Last Name</label>
            <input type="text" name="lname" placeholder="Last Name">
            <label>Email</label>
            <input type="email" name="email" placeholder="Your Email Address">
            <label>Phone Number</label>
            <input type="text" name="fname">
        </form>
    </div>
    <hr>
    <strong><center>Let's begin here</center></strong>
    <hr>
    <input type="text" name="search" placeholder="Enter your search keyword">
    <br><br>
    <textarea placeholder="Please enter your comments."></textarea>
    <button>Say Hello!</button>

</body>
</html>

So, how can I append a value attribute to each of the input tags in this HTML file using BeautifulSoup?

Upvotes: 0

Views: 418

Answers (1)

chitown88
chitown88

Reputation: 28575

You can simply add it the same as you would for key/value in a dictionary:

test_html:

test_html = '''<!DOCTYPE html>
<html>
<head>
    <title>Testing Accessibility</title>
</head>
<body>

    <h1>Hello World</h1>

    <div>
        <h2>This is a title</h2>
        <p>Welcome, to the world's first <strong>Accessibility Script</strong>. You will be amazed by it completely!<br>Hope you'll enjoy it!</p>
    </div>
    <hr>
    <div>
        <form>
            <label>First Name</label>
            <input type="text" name="fname" placeholder="First Name">
            <label>Last Name</label>
            <input type="text" name="lname" placeholder="Last Name">
            <label>Email</label>
            <input type="email" name="email" placeholder="Your Email Address">
            <label>Phone Number</label>
            <input type="text" name="fname">
        </form>
    </div>
    <hr>
    <strong><center>Let's begin here</center></strong>
    <hr>
    <input type="text" name="search" placeholder="Enter your search keyword">
    <br><br>
    <textarea placeholder="Please enter your comments."></textarea>
    <button>Say Hello!</button>

</body>
</html>'''

Code:

from bs4 import BeautifulSoup

soup = BeautifulSoup(test_html, 'html.parser')
input_tags = soup.find_all('input')

for each in input_tags:
    each['value'] = 'some_value'

Output:

print(str(soup))
<!DOCTYPE html>

<html>
<head>
<title>Testing Accessibility</title>
</head>
<body>
<h1>Hello World</h1>
<div>
<h2>This is a title</h2>
<p>Welcome, to the world's first <strong>Accessibility Script</strong>. You will be amazed by it completely!<br/>Hope you'll enjoy it!</p>
</div>
<hr/>
<div>
<form>
<label>First Name</label>
<input name="fname" placeholder="First Name" type="text" value="some_value"/>
<label>Last Name</label>
<input name="lname" placeholder="Last Name" type="text" value="some_value"/>
<label>Email</label>
<input name="email" placeholder="Your Email Address" type="email" value="some_value"/>
<label>Phone Number</label>
<input name="fname" type="text" value="some_value"/>
</form>
</div>
<hr/>
<strong><center>Let's begin here</center></strong>
<hr/>
<input name="search" placeholder="Enter your search keyword" type="text" value="some_value"/>
<br/><br/>
<textarea placeholder="Please enter your comments."></textarea>
<button>Say Hello!</button>
</body>
</html>

Upvotes: 1

Related Questions