콘솔워크

python 파이썬으로 html 코드에서 주석만 추출하는 방법 remove <!-- --> beatuifulsoup4 사용 본문

프로그래밍/python

python 파이썬으로 html 코드에서 주석만 추출하는 방법 remove <!-- --> beatuifulsoup4 사용

콘솔워크 2022. 4. 9. 10:32
반응형
from bs4 import BeautifulSoup, Comment
soup = BeautifulSoup("""1<!--The loneliest number-->
                        <a>2<!--Can be as bad as one--><b>3""")
comments = soup.findAll(text=lambda text:isinstance(text, Comment))
comments_tags = [comment.extract() for comment in comments]

위의 코드를 날리면, 특정 html 코드에 있는 주석 부분만 추출해준다.
참조: https://code-examples.net/ko/q/358453

 

 

 

먼저 beatuifulsoup4가 없다면 먼저 설치해줍니다.

 

pip install beautifulsoup4

 

설치가 완료되면 import 하여 사용합니다.

from bs4 import BeautifulSoup, Comment


def get_html_without_comment(self):
    driver = self.driver

    p_detail_html = ""
    # 상세설명 가져오기
    p_detail_html = driver.find_element(By.CSS_SELECTOR, 'div.prpv_gosiwrap').get_attribute(
        "outerHTML").replace("\n", "").replace("\t", "")

    soup_p_detail_html = BeautifulSoup(p_detail_html)
    comments = soup_p_detail_html.findAll(
        text=lambda text: isinstance(text, Comment))

    for comment in comments:
        replace_text = comment.extract()
        p_detail_html = p_detail_html.replace(replace_text, "")
        p_detail_html = p_detail_html.replace("<!---->", "")
    return p_detail_html

 

그것을 응용하여 특정 html을 가져온다음에 여기서 주석 부분 (<!-- 주석입니다 -->) 이런 코드들을 모두 제거하고싶으면 이렇게 하면 된다.

반응형