1

我想将字符串与 html 页面的内容进行比较。但是 HTML 页面中的特殊字符使这种比较变得更加困难。所以我想在比较之前从 HTML 页面中删除所有特殊字符和空格。但所有标签必须保持不变。那是

<div class="abc bcd">
         <div  class="inner1"> Hai ! this is first inner div;</div>
         <div class="inner2"> "this is second div... " </div>
</div>

这应该转换为

<div class="abc bcd">
          <div class="inner1">Haithisisfirstinnerdiv</div>
          <div class="inner2">thisisseconddiv</div>
</div>

如何做到这一点?

4

2 回答 2

1

找到所有叶子标签并更改它们的字符串。

alphabet = 'abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

def replace(soup):
    for child in soup.children:
        if child.string:
            child.string = ''.join([ch for ch in child.string if ch in alphabet])
        else:
            replace(child)

from bs4 import BeautifulSoup

orig_string = """
<div class="abc bcd">
         <div  class="inner1"> Hai ! this is first inner div;</div>
         <div class="inner2"> "this is second div... " </div>
</div> """

soup = BeautifulSoup(orig_string)
print soup.prettify() # original HTML
replace(soup)
print
print soup.prettify() # new HTML

输出:

<html>
 <body>
  <div class="abc bcd">
   <div class="inner1">
    Hai ! this is first inner div;
   </div>
   <div class="inner2">
    "this is second div... "
   </div>
  </div>
 </body>
</html>

<html>
 <body>
  <div class="abc bcd">
   <div class="inner1">
    Haithisisfirstinnerdiv
   </div>
   <div class="inner2">
    thisisseconddiv
   </div>
  </div>
 </body>
</html>
于 2013-05-12T04:26:38.260 回答
0

首先,在调用SoBeautifulSoup时已经修复了一些损坏的 HTML :BeautifulSoup()

<div  class="inner1"> 

前往

<div class="inner1">

以下是如何摆脱空白和特殊字符:

>>> from bs4 import BeautifulSoup
>>> html = """<div class="abc bcd">
     <div  class="inner1"> Hai ! this is first inner div;</div>
     <div class="inner2"> "this is second div... " </div>
</div>""" 
>>> soup = BeautifulSoup(html)
>>> for divtag in soup.findAll('div'):
...     if 'inner' in divtag['class'][0]:
...         divtag.string = ''.join(i for i in divtag.string if i.isalnum())
>>> print soup
<html><body><div class="abc bcd">
<div class="inner1">Haithisisfirstinnerdiv</div>
<div class="inner2">thisisseconddiv</div>
</div></body></html>
于 2013-05-12T04:32:59.083 回答