Ivan Gonzalez
Ivan Gonzalez

Reputation: 576

Extracting text without tags of HTML with Beautifulsoup Python

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

Upvotes: 1

Views: 6455

Answers (1)

harshil9968
harshil9968

Reputation: 3244

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

Upvotes: 4

Related Questions