How can I search AND replace the text in a soup object?

Viewed 76

I have an html table with content like this in multiple rows

<td>
              Bob Marley (bmarley@whatever.com)
</td>

I'm trying to find and replace this content with just the first part of the email address, and strip the white space.

<td>
bmarley
</td>

This is the current code, that successfully matches on the username portion. I'm not sure how to replace the content between the tags.

name_regex = re.compile('(?<=())[a-zA-Z]+(?=@)')
soup = bs(open(page_source), 'lxml')
for row in soup.select(table_name)[1:]:
    name = re.search(name_regex,row.select_one('td:nth-of-type(2)').text.strip()).group()
    print(name)
1 Answers

You can assign to .string property, for example:

import re
from bs4 import BeautifulSoup


html_doc = """
<td>
              Bob Marley (bmarley@whatever.com)
</td>
<td>
              John Smith (j.smith@whatever.com)
</td>"""

soup = BeautifulSoup(html_doc, "html.parser")

name_regex = re.compile(r"\((.*)@")

for td in soup.select("td"):
    td.string = name_regex.search(td.text).group(1)

print(soup)

Prints:

<td>bmarley</td>
<td>j.smith</td>
Related