LXML Xpath 기능을 사용하여 HTML 파서를 작성 중입니다. 그것은 잘 작동하는 것,하지만 나는 하나의 주요 문제가 있습니다.Python - lxml Xpath 기능으로 일부 HTML 태그 유지하기
모든 HTML <p>
태그를 구문 분석 할 때 <b>
, <i>
등의 태그를 사용하는 단어가 있습니다. 이러한 태그는 유지해야합니다.
예를 들어, HTML을 구문 분석 할 때;
<div class="ArticleDetail">
<p>Hello world, this is a <b>simple</b> test, which contains words in <i>italic</i> and others.
I have a <strong>strong</strong> tag here. I guess this is a silly test.
<br/>
Ops, line breaks.
<br/></p>
이 파이썬 코드를 실행하면;
x = lxml.html.fromstring("...html text...").xpath("//div[@class='ArticleDetail']/p")
for stuff in x:
print stuff.text_content()
이 방법은 정상적으로 작동하지만 p 대신 다른 모든 태그를 제거합니다.
출력 :
Hello world, this is a simple test, which contains words in italic and others.
I have a strong tag here. I guess this is a silly test.
Ops, line breaks.
당신이 모든 <b>
, <i>
및 <strong>
태그를 제거 볼 수 있듯이. 어쨌든 그들을 지킬 수 있습니까?
덕분에, 코드 샘플에 해당 태그를 추가하는 것을 잊었다. –