Myzel394
Myzel394

Reputation: 1317

Beautifulsoup - Wrap first character in <span>

How can I wrap the first character in a tag using Beautifulsoup? I have a normal html document and I get the first p tag and the first character should be wrapped in a <span> tag (Space doesn't count as character, it shouldn't wrap space).

Example:

<p>
    Lorem Ipsum blablabla
</p>

This should be changed to this:

<p>
    <span class="first_word">L</span>orem Ipsum blablabla
</p>

Upvotes: 1

Views: 181

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195408

.new_tag() creates new tag and with .string property you can modify underlying text. For example:

from bs4 import BeautifulSoup

txt = '''<p>
    Lorem Ipsum blablabla
</p>'''

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

text, new_tag = soup.p.text.strip(), soup.new_tag("span", **{"class": "first_word"})
new_tag.string, soup.p.string = text[0], text[1:]

soup.p.string.insert_before(new_tag)

print(soup)

Prints:

<p><span class="first_word">L</span>orem Ipsum blablabla</p>

Upvotes: 2

Eduardo
Eduardo

Reputation: 4382

You can use something like the code below, i.e., creates a new span tag with the desired class then injects it into the HTML body. Some string manipulation is needed in order to put the L into this new tag and remove it from the existing text.

from bs4 import BeautifulSoup

markup = '<p>    Lorem Ipsum blablabla</p>'
soup = BeautifulSoup(markup, "html.parser")

new_span = soup.new_tag("span", **{"class": "first_word"})
new_span.string = soup.p.string.strip()[0]

soup.p.string = soup.p.string.strip()[1:]
soup.p.string.insert_before(new_span)
print(soup)

Execute it and it will print

<p><span class="first_word">L</span>orem Ipsum blablabla</p>

Upvotes: 2

Related Questions