使用Beautifulsoup Python提取没有HTML标签的文本 [英] Extracting text without tags of HTML with Beautifulsoup Python
本文介绍了使用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
这篇关于使用Beautifulsoup Python提取没有HTML标签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文