简体   繁体   中英

BeautifulSoup: how to replace content with in span tag

........<p style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; text-indent:0px;">textHere

<span style=" font-family:'Noto Sans';">ABC</span></p>

<p style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; text-indent:0px;"><span style=" font.......

I have a HTML like above. I need to

  1. find all the content in 'Noto Sans' font-family (they are always inside span tag)
  2. replace them (A as X, B as Y etc…) with out changing rest of the code

what I tried is this, but not working properly.

from bs4 import BeautifulSoup
source_code = """.....<span style=" font-family:'Noto Sans';">ABC</span></p>......""
soup = BeautifulSoup(source_code, "lxml")

for re in soup.findAll('font', 'face' = "Noto Sans"):
    print (re.replace("A", "X"))

Any thoughts ?

You need to find all span tags having font-family: Noto Sans inside, then replace the A with X inside every span element that you've found:

import re

from bs4 import BeautifulSoup


source_code = """.....<span style=" font-family:'Noto Sans';">ABC</span></p>......"""    
soup = BeautifulSoup(source_code, "lxml")

for elm in soup.find_all('span', style=re.compile(r"font-family:'Noto Sans'")):
    elm.string = elm.text.replace("A", "X")

print(soup.prettify())

Prints:

<span style=" font-family:'Noto Sans';">
 XBC
</span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM