2011-09-22 4 views
2

Beautiful Soup로 파싱하기 전에 깨진 HTML 태그를 수정하는 방법을 알고 싶습니다.아름다운 스프 - 깨진 태그를 고치는 방법

다음 스크립트에서 td><td으로 바꿔야합니다.

Beautiful Soup에서 볼 수있는 대체 방법은 무엇입니까? (작업)

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 태그의 전체 (적어도이 완료되어야합니다) 목록을 잡았다. ( </endtag>를)

>>> 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> 

이 일뿐만 아니라 깨진 끝 태그와 일치해야합니다 :

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) 

가 생산 : 그것을 밖으로 시도

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를 수정하고 내 대답을 편집하여 지금 내가 가지고있는 re.sub를 시험해보십시오. 나는 그것을 테스트하고 꽤 잘 작동합니다. 모든 html 태그의 전체 목록을 확인합니다 (w3에서 가져옴). – chown

2

을 그 '유일한 것은 당신이라면 td> ->에 관심이 있으시면 시도하십시오 :

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

myString을 BeautifulSoup로 보내기 전에. 다른 깨진 태그가있는 경우 몇 가지 예를 들면 우리가 작업 해 보겠습니다.)

관련 문제