用BeautifulSoup中的另一个标签替换一种标签

问题描述:

我有一个HTML文件集合。我希望一个一个地遍历它们,编辑特定类的标记。我想编辑的代码是以下形式,使用下面的类名称:用BeautifulSoup中的另一个标签替换一种标签

<td class='thisIsMyClass' colspan=4> 
    <a id='123' class='thisIsMyOtherClass' href='123'>Put me Elsewhere</a> 

这可以在同一文档中出现多次,用不同的文本,而不是“把我在别处”,但始终不变类。

我想改变这是下面的形式:

<font SIZE="3" COLOR="#333333" FACE="Verdana" STYLE="background-color:#ffffff;font-weight: bold;"> 
    <h2>Put Me Elsewhere</h2> 
</font> 
import os 
for filename in os.listdir('dirname'): 
replace(filename) 

def replace(filename): 
tags = soup.find_all(attrs={"thisIsMyClass"}) 

不太清楚在这之后去哪里或如何处理标签阵列?任何帮助将非常感激。谢谢:)

+0

HTML对

元素的儿童有一些限制。您可能只想考虑更换标签。如果由于其属性需要擦除​​,可能将其替换为普通的​​会比完全删除它们更好。 – tiffon2014-12-07 01:57:57

回答

好得多,更漂亮将是一个占位符准备替换HTML字符串,找到所有td标签与thisIsMyClass类,并使用.replace_with()来代替每个:

from bs4 import BeautifulSoup 

data = """ 
<table> 
    <tr> 
     <td class='thisIsMyClass' colspan=4> 
      <a id='123' class='thisIsMyOtherClass' href='123'>Put me Elsewhere</a> 
     </td> 
    </tr> 
</table> 
""" 

replacement = """ 
<font SIZE="3" COLOR="#333333" FACE="Verdana" STYLE="background-color:#ffffff;font-weight: bold;"> 
    <h2>{text}</h2> 
</font> 
""" 

soup = BeautifulSoup(data, 'html.parser') 
for td in soup.select('td.thisIsMyClass'): 
    td.replace_with(BeautifulSoup(replacement.format(text=td.a.text), 'html.parser')) 

print soup.prettify() 

打印:

<table> 
    <tr> 
     <font color="#333333" face="Verdana" size="3" style="background-color:#ffffff;font-weight: bold;"> 
      <h2> 
      Put me Elsewhere 
      </h2> 
     </font> 
    </tr> 
</table> 

这就像分配给name属性一样简单。

# for quick testing: 
# tag = BeautifulSoup("<td class='thisIsMyClass' colspan=4><a id='123' class='thisIsMyOtherClass' href='123'>Put me Elsewhere</a>") 
# tags = [tag] 
for tag in tags: 
    tag.td.name = "font" 
    tag.font["SIZE"] = 3 
    del tag.font["class"] 
    ... 
    tag.a.name = "h2" 
    ... 
    print(tag) 
    # <font SIZE="3" colspan="4"><h2 class="thisIsMyOtherClass" href="123" id="123">Put me Elsewhere</h2></font> 

另外documentation是你的朋友。这是相当全面的。

好得多,更漂亮将是一个占位符准备替换HTML字符串,找到所有td标签与thisIsMyClass类,并使用.replace_with()来代替每个:

from bs4 import BeautifulSoup 

data = """ 
<table> 
    <tr> 
     <td class='thisIsMyClass' colspan=4> 
      <a id='123' class='thisIsMyOtherClass' href='123'>Put me Elsewhere</a> 
     </td> 
    </tr> 
</table> 
""" 

replacement = """ 
<font SIZE="3" COLOR="#333333" FACE="Verdana" STYLE="background-color:#ffffff;font-weight: bold;"> 
    <h2>{text}</h2> 
</font> 
""" 

soup = BeautifulSoup(data, 'html.parser') 
for td in soup.select('td.thisIsMyClass'): 
    td.replace_with(BeautifulSoup(replacement.format(text=td.a.text), 'html.parser')) 

print soup.prettify() 

打印:

<table> 
    <tr> 
     <font color="#333333" face="Verdana" size="3" style="background-color:#ffffff;font-weight: bold;"> 
      <h2> 
      Put me Elsewhere 
      </h2> 
     </font> 
    </tr> 
</table> 

这就像分配给name属性一样简单。

# for quick testing: 
# tag = BeautifulSoup("<td class='thisIsMyClass' colspan=4><a id='123' class='thisIsMyOtherClass' href='123'>Put me Elsewhere</a>") 
# tags = [tag] 
for tag in tags: 
    tag.td.name = "font" 
    tag.font["SIZE"] = 3 
    del tag.font["class"] 
    ... 
    tag.a.name = "h2" 
    ... 
    print(tag) 
    # <font SIZE="3" colspan="4"><h2 class="thisIsMyOtherClass" href="123" id="123">Put me Elsewhere</h2></font> 

另外documentation是你的朋友。这是相当全面的。

好得多,更漂亮将是一个占位符准备替换HTML字符串,找到所有td标签与thisIsMyClass类,并使用.replace_with()来代替每个:

from bs4 import BeautifulSoup 

data = """ 
<table> 
    <tr> 
     <td class='thisIsMyClass' colspan=4> 
      <a id='123' class='thisIsMyOtherClass' href='123'>Put me Elsewhere</a> 
     </td> 
    </tr> 
</table> 
""" 

replacement = """ 
<font SIZE="3" COLOR="#333333" FACE="Verdana" STYLE="background-color:#ffffff;font-weight: bold;"> 
    <h2>{text}</h2> 
</font> 
""" 

soup = BeautifulSoup(data, 'html.parser') 
for td in soup.select('td.thisIsMyClass'): 
    td.replace_with(BeautifulSoup(replacement.format(text=td.a.text), 'html.parser')) 

print soup.prettify() 

打印:

<table> 
    <tr> 
     <font color="#333333" face="Verdana" size="3" style="background-color:#ffffff;font-weight: bold;"> 
      <h2> 
      Put me Elsewhere 
      </h2> 
     </font> 
    </tr> 
</table> 

这就像分配给name属性一样简单。

# for quick testing: 
# tag = BeautifulSoup("<td class='thisIsMyClass' colspan=4><a id='123' class='thisIsMyOtherClass' href='123'>Put me Elsewhere</a>") 
# tags = [tag] 
for tag in tags: 
    tag.td.name = "font" 
    tag.font["SIZE"] = 3 
    del tag.font["class"] 
    ... 
    tag.a.name = "h2" 
    ... 
    print(tag) 
    # <font SIZE="3" colspan="4"><h2 class="thisIsMyOtherClass" href="123" id="123">Put me Elsewhere</h2></font> 

另外documentation是你的朋友。这是相当全面的。