Extracting text without tags of HTML with Beautifulsoup Python

心不动则不痛 提交于 2019-12-23 04:24:39

问题


I try to extract this part of text but i don't figure it out how to do it, i'm working with several html files locally.

<HTML><HEAD><STYLE>SOME STYLE CODE</STYLE></HEAD><META http-equiv=Content-Type content="text/html; charset=utf-8">
<BODY>
<H1>SOME TEXT I DONT WANT</H1>
THIS TEXT IS WHICH I WANT
<H1>ANOTHER TEXT I DONT WANT</H1>
ANOTHER TEXT THAT I WANT
[.. Continues ..]
</BODY></HTML>

Thanks for your help

EDIT: I have tried with this code but sometimes prints the h1 tags

import glob
from bs4 import BeautifulSoup
for file in glob.glob('Logs/Key*.html'):
    with open(file) as f:
        htmlfile = f.read()
        soup = BeautifulSoup(htmlfile, 'html.parser')
        c = soup.find('body').findAll()
        for i in c:
            print i.nextSibling

EDIT 2: Actually the problem is that the html file has only one line, so when i try to run that code with this html, also prints the h1 tags:

from bs4 import BeautifulSoup
htmlfile = '<HTML><HEAD><STYLE>SOME STYLE CODE</STYLE></HEAD><META http-equiv=Content-Type content="text/html; charset=utf-8"><BODY><H1>SHIT</H1>WANTED<H1>SHIT</H1><H1>SHIT</H1>WANTED<H1>SHIT</H1>WANTED<H1>SHIT</H1>WANTED</BODY><HTML>'
soup = BeautifulSoup(htmlfile, 'html.parser')
c = soup.find('body').findAll()
for i in c:
    print i.nextSibling

Prints:

WANTED
<h1>SHIT</h1>
WANTED
WANTED
WANTED

回答1:


Now you can put HTML_TEXT as the html you got from scrapping the url.

y = BeautifulSoup(HTML_TEXT)

c = y.find('body').findAll(text=True, recursive=False)

for i in c:
    print i


来源:https://stackoverflow.com/questions/38859271/extracting-text-without-tags-of-html-with-beautifulsoup-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!