2011-09-22 10 views
2

美しいスープで解析する前に壊れたhtmlタグを修正する方法を知っておきたいと思います。美しいスープ - 壊れたタグを修正する方法

次のスクリプトでは、td><tdに置き換える必要があります。

私は美しいスープがそれを見ることができるようにどのように置換を行うことができますか?

from BeautifulSoup import BeautifulSoup 

s = """ 
<tr> 
td>LABEL1</td><td>INPUT1</td> 
</tr> 
<tr> 
<td>LABEL2</td><td>INPUT2</td> 
</tr>""" 

a = BeautifulSoup(s) 

left = [] 
right = [] 

for tr in a.findAll('tr'): 
    l, r = tr.findAll('td') 
    left.extend(l.findAll(text=True)) 
    right.extend(r.findAll(text=True)) 

print left + right 

答えて

2

編集(作業):

私は照合するW3からすべてのHTMLタグの完全な(少なくとも、それは完全でなければなりません)リストをつかみました。それを試してみてください:

fixedString = re.sub(">\s*(\!--|\!DOCTYPE|\ 
          a|abbr|acronym|address|applet|area|\ 
          b|base|basefont|bdo|big|blockquote|body|br|button|\ 
          caption|center|cite|code|col|colgroup|\ 
          dd|del|dfn|dir|div|dl|dt|\ 
          em|\ 
          fieldset|font|form|frame|frameset|\ 
          head|h1|h2|h3|h4|h5|h6|hr|html|\ 
          i|iframe|img|input|ins|\ 
          kbd|\ 
          label|legend|li|link|\ 
          map|menu|meta|\ 
          noframes|noscript|\ 
          object|ol|optgroup|option|\ 
          p|param|pre|\ 
          q|\ 
          s|samp|script|select|small|span|strike|strong|style|sub|sup|\ 
          table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|\ 
          u|ul|\ 
          var)>", "><\g<1>>", s) 
bs = BeautifulSoup(fixedString) 

が生成されます

>>> print s 

<tr> 
td>LABEL1</td><td>INPUT1</td> 
</tr> 
<tr> 
<td>LABEL2</td><td>INPUT2</td> 
</tr> 

>>> print re.sub(">\s*(\!--|\!DOCTYPE|\ 
         a|abbr|acronym|address|applet|area|\ 
         b|base|basefont|bdo|big|blockquote|body|br|button|\ 
         caption|center|cite|code|col|colgroup|\ 
         dd|del|dfn|dir|div|dl|dt|\ 
         em|\ 
         fieldset|font|form|frame|frameset|\ 
         head|h1|h2|h3|h4|h5|h6|hr|html|\ 
         i|iframe|img|input|ins|\ 
         kbd|\ 
         label|legend|li|link|\ 
         map|menu|meta|\ 
         noframes|noscript|\ 
         object|ol|optgroup|option|\ 
         p|param|pre|\ 
         q|\ 
         s|samp|script|select|small|span|strike|strong|style|sub|sup|\ 
         table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|\ 
         u|ul|\ 
         var)>", "><\g<1>>", s) 

<tr><td>LABEL1</td><td>INPUT1</td> 
</tr> 
<tr> 
<td>LABEL2</td><td>INPUT2</td> 
</tr> 

この1は、同様に(</endtag>を)壊れた終了タグと一致する必要があります。

re.sub(">\s*(/?)(\!--|\!DOCTYPE|\a|abbr|acronym|address|applet|area|\ 
       b|base|basefont|bdo|big|blockquote|body|br|button|\ 
       caption|center|cite|code|col|colgroup|\ 
       dd|del|dfn|dir|div|dl|dt|\ 
       em|\ 
       fieldset|font|form|frame|frameset|\ 
       head|h1|h2|h3|h4|h5|h6|hr|html|\ 
       i|iframe|img|input|ins|\ 
       kbd|\ 
       label|legend|li|link|\ 
       map|menu|meta|\ 
       noframes|noscript|\ 
       object|ol|optgroup|option|\ 
       p|param|pre|\ 
       q|\ 
       s|samp|script|select|small|span|strike|strong|style|sub|sup|\ 
       table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|\ 
       u|ul|\ 
       var)>", "><\g<1>\g<2>>", s) 
+0

運がいいです。出力:<><><><<><><><><><><<><><> – howtodothis

+1

@terra @ regexを修正し、私の答えを編集しました。私はそれをテストし、それはかなりうまくいくはずです。それは、すべてのhtmlタグの完全なリストをチェックします(w3から引き出されます)。 – chown

2

それはあなた」だけのことだ場合td> - >と心配して、試してみてください:

myString = re.sub('td>', '<td>', myString) 

myStringをBeautifulSoupに送信する前に。他に壊れているタグがある場合は、いくつかの例を挙げてお答えします。)

関連する問題