使用Beautifulsoup Python提取没有HTML标签的文本

问题描述:

我尝试提取文本的这一部分,但我不知道该怎么做,我正在本地处理多个html文件.

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>

感谢您的帮助

我已经尝试过使用此代码,但有时会打印h1标签

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

实际上,问题在于html文件只有一行,因此当我尝试使用此html运行该代码时,还会打印h1标签:

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

打印:

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

现在,您可以将HTML_TEXT用作通过抓取网址获得的html.

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