텍스트, 표 및 html로 여러 웹 페이지를 구문 분석하려고합니다. 모든 페이지의 단락 수는 다르지만 모든 단락은 <div>
으로 시작하지만 끝까지 </div>
은 종료되지 않습니다. text1 <b>text2</b> (table_deleted) text3
BeautifulSoup을 사용하여 중첩 된 div 구문 분석
실제 결과 text1\n\ntext2some text heretext 3text2some text heretext 3 (table deleted)
from bs4 import BeautifulSoup
html = """
<h1>title</h1>
<h3>extra data</h3>
<div>
text1
<div>
<b>next2</b><table>some text here</table>text 3
</div>
</div>"""
soup = BeautifulSoup(html, 'html5lib')
tags = soup.find('h3').find_all_next()
contents = ""
for tag in tags:
if tag.name == 'table':
contents += " (table deleted) "
contents += tag.text.strip()
print(contents)
을 사용하여 "테이블"태그를 대체하고 변경할 수 있습니다, 왜 구문 분석 선조? – putonspectacles
허. 나는 나 자신을 위해 어려운 일을하는 것을 좋아한다. 그것은 정말로 효과가 있습니다. :) – bluppfisk