Python을 사용하여 HTML 파일에서 텍스트 추출 브라우저에서 텍스트를 복사하여 메모장에 붙여 넣으면

파이썬을 사용하여 HTML 파일에서 텍스트를 추출하고 싶습니다. 브라우저에서 텍스트를 복사하여 메모장에 붙여 넣으면 본질적으로 동일한 결과를 원합니다.

형식이 잘못된 HTML에서 실패 할 수있는 정규 표현식을 사용하는 것보다 더 강력한 것을 원합니다. 많은 사람들이 뷰티플 수프를 추천하는 것을 보았지만 사용하는데 몇 가지 문제가있었습니다. 우선, JavaScript 소스와 같은 원치 않는 텍스트를 선택했습니다. 또한 HTML 엔터티를 해석하지 않았습니다. 예를 들어 & # 39; 브라우저 소스를 메모장에 붙여 넣은 것처럼 HTML 소스에서 텍스트의 아포스트로피로 변환됩니다.

업데이트 html2text 가 유망 해 보입니다. HTML 엔터티를 올바르게 처리하고 JavaScript를 무시합니다. 그러나 정확하게 일반 텍스트를 생성하지는 않습니다. 마크 다운을 생성 한 다음 일반 텍스트로 바꿔야합니다. 예제 나 문서는 없지만 코드는 깨끗해 보입니다.


관련 질문 :



답변

html2text 는 이것에서 꽤 잘하는 파이썬 프로그램입니다.


답변

자바 스크립트를 얻거나 원하지 않는 것을 얻지 않고 텍스트를 추출하기 위해 찾은 최고의 코드 조각 :

import urllib
from bs4 import BeautifulSoup

url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)

# kill all script and style elements
for script in soup(["script", "style"]):
    script.extract()    # rip it out

# get text
text = soup.get_text()

# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)

print(text)

다음 이전에 BeautifulSoup을 설치해야합니다.

pip install beautifulsoup4

답변

참고 : NTLK는 더 이상 clean_html기능을 지원하지 않습니다

아래의 원래 답변과 의견 섹션의 대안.


NLTK 사용

html2text 문제를 해결하는 데 4-5 시간을 낭비했습니다. 운 좋게도 NLTK를 만날 수있었습니다.
마술처럼 작동합니다.

import nltk
from urllib import urlopen

url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"
html = urlopen(url).read()
raw = nltk.clean_html(html)
print(raw)

답변

오늘도 같은 문제에 직면했습니다. 나는 모든 마크 업의 들어오는 내용을 제거하기 위해 매우 간단한 HTML 파서를 작성하여 최소한의 서식으로 나머지 텍스트를 반환했습니다.

from HTMLParser import HTMLParser
from re import sub
from sys import stderr
from traceback import print_exc

class _DeHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.__text = []

    def handle_data(self, data):
        text = data.strip()
        if len(text) > 0:
            text = sub('[ \t\r\n]+', ' ', text)
            self.__text.append(text + ' ')

    def handle_starttag(self, tag, attrs):
        if tag == 'p':
            self.__text.append('\n\n')
        elif tag == 'br':
            self.__text.append('\n')

    def handle_startendtag(self, tag, attrs):
        if tag == 'br':
            self.__text.append('\n\n')

    def text(self):
        return ''.join(self.__text).strip()


def dehtml(text):
    try:
        parser = _DeHTMLParser()
        parser.feed(text)
        parser.close()
        return parser.text()
    except:
        print_exc(file=stderr)
        return text


def main():
    text = r'''
        <html>
            <body>
                <b>Project:</b> DeHTML<br>
                <b>Description</b>:<br>
                This small script is intended to allow conversion from HTML markup to
                plain text.
            </body>
        </html>
    '''
    print(dehtml(text))


if __name__ == '__main__':
    main()

답변

다음은 약간 더 완전한 xperroni의 답변 버전입니다. 스크립트 및 스타일 섹션을 건너 뛰고 문자 참조 (예 : & # 39;) 및 HTML 엔티티 (예 : & amp;)를 번역합니다.

또한 사소한 일반 텍스트 -html 역변환 기가 포함되어 있습니다.

"""
HTML <-> text conversions.
"""
from HTMLParser import HTMLParser, HTMLParseError
from htmlentitydefs import name2codepoint
import re

class _HTMLToText(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self._buf = []
        self.hide_output = False

    def handle_starttag(self, tag, attrs):
        if tag in ('p', 'br') and not self.hide_output:
            self._buf.append('\n')
        elif tag in ('script', 'style'):
            self.hide_output = True

    def handle_startendtag(self, tag, attrs):
        if tag == 'br':
            self._buf.append('\n')

    def handle_endtag(self, tag):
        if tag == 'p':
            self._buf.append('\n')
        elif tag in ('script', 'style'):
            self.hide_output = False

    def handle_data(self, text):
        if text and not self.hide_output:
            self._buf.append(re.sub(r'\s+', ' ', text))

    def handle_entityref(self, name):
        if name in name2codepoint and not self.hide_output:
            c = unichr(name2codepoint[name])
            self._buf.append(c)

    def handle_charref(self, name):
        if not self.hide_output:
            n = int(name[1:], 16) if name.startswith('x') else int(name)
            self._buf.append(unichr(n))

    def get_text(self):
        return re.sub(r' +', ' ', ''.join(self._buf))

def html_to_text(html):
    """
    Given a piece of HTML, return the plain text it contains.
    This handles entities and char refs, but not javascript and stylesheets.
    """
    parser = _HTMLToText()
    try:
        parser.feed(html)
        parser.close()
    except HTMLParseError:
        pass
    return parser.get_text()

def text_to_html(text):
    """
    Convert the given text to html, wrapping what looks like URLs with <a> tags,
    converting newlines to <br> tags and converting confusing chars into html
    entities.
    """
    def f(mo):
        t = mo.group()
        if len(t) == 1:
            return {'&':'&amp;', "'":'&#39;', '"':'&quot;', '<':'&lt;', '>':'&gt;'}.get(t)
        return '<a href="%s">%s</a>' % (t, t)
    return re.sub(r'https?://[^] ()"\';]+|[&\'"<>]', f, text)

답변

나는 이미 많은 답변이 있다는 것을 알고 있지만, 내가 찾은 가장 우아 하고 파이썬적인 해결책은 부분적으로 여기 에 설명되어 있습니다 .

from bs4 import BeautifulSoup

text = ''.join(BeautifulSoup(some_html_string, "html.parser").findAll(text=True))

최신 정보

프레이저의 의견을 바탕으로보다 우아한 솔루션이 있습니다.

from bs4 import BeautifulSoup

clean_text = ''.join(BeautifulSoup(some_html_string, "html.parser").stripped_strings)

답변

stripogram 라이브러리에서도 html2text 메소드를 사용할 수 있습니다.

from stripogram import html2text
text = html2text(your_html_string)

스트립 그램을 설치하려면 sudo easy_install stripogram을 실행하십시오.