Python에서 HTML, XML의 태그를 제거한 텍스트 추출 방법

2021. 2. 12. 23:47서버 프로그래밍

역시나 BeautifulSoup를 이용하면 간단하게 처리가 된다. 

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)

 

stackoverflow.com/questions/37018475/python-remove-all-html-tags-from-string/37019031

 

Python, remove all html tags from string

I am trying to access the article content from a website, using beautifulsoup with the below code: site= 'www.example.com' page = urllib2.urlopen(req) soup = BeautifulSoup(page) content = soup.fin...

stackoverflow.com