本文介绍了使用Python将html转换为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python将html块转换为文本.

I am trying to convert an html block to text using Python.

输入:

<div class="body"><p><strong></strong></p>
<p><strong></strong>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>
<p>Consectetuer adipiscing elit. <a href="http://example.com/" target="_blank" class="source">Some Link</a> Aenean commodo ligula eget dolor. Aenean massa</p>
<p>Aenean massa.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>
<p>Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p></div>

所需的输出:

定理 ipsum dolor坐在amet,秘书adipiscing精英.艾尼安(Aenean commodo) ligula eget dolor.马尼(Aenean massa)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa

圣贤教士精锐. 一些 链接Aenean commodo ligula eget dolor.艾南(Aenean massa)

Consectetuer adipiscing elit. Some Link Aenean commodo ligula eget dolor. Aenean massa

艾南(Aenean) Massa.Lorem ipsum dolor坐着,安全管教精英.艾尼安(Aenean) commodo ligula eget dolor. Aenean Massa

Aenean massa.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa

Lorem ipsum dolor sit 敬业,奉献的精英人士. Aenean commodo ligula eget dolor. 马尼(Aenean massa)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa

奉献者奉献精英.艾尼安·科莫多 ligula eget dolor.马尼拉(Aenean Massa)

Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa

我尝试使用html2text模块没有太大的成功(我对python还是很陌生的:))

I have tried using html2text module without much success (i am quite new to python :))

这是我尝试过的:

#!/usr/bin/env python

import urllib2
import html2text
from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(urllib2.urlopen('http://example.com/page.html').read())

txt = soup.find('div', {'class' : 'body'})

print html2text.html2text(txt)

"txt"对象产生上面的html块.我想将其转换为文本并在屏幕上打印.

the "txt" object produces the html block above. I'd like to convert it to text and print it on the screen.

对代码段的任何帮助将不胜感激.

Any help with the piece of code would be much appreciated.

推荐答案

我缺少什么? soup.get_text()给出与您想要的输出完全相同的输出...

What am I missing? soup.get_text() gives exactly the same output you wanted...

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print(soup.get_text())

输出

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Consectetuer adipiscing elit. Some Link Aenean commodo ligula eget dolor. Aenean massa
Aenean massa.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa

编辑-并保留换行符,如@ t-8ch所指出:

EDIT - And to keep newlines, as pointed out by @t-8ch:

print(soup.get_text('\n'))

PS!确切地说,您可以将换行符替换为双换行符-然后与您的示例相同:)

PS! To be exact you can replace newline with a double one -- then it is identical to your example :)

soup.get_text().replace('\n','\n\n')

这篇关于使用Python将html转换为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:54