问题描述
我正在尝试使用 Python 将 html 块转换为文本.
输入:
<p><strong></strong>Lorem ipsum dolor sat amet,consectetuer adipiscing 精英.Aenean commodo ligula eget dolor.Aenean massa </p><p>Consectetuer adipiscing 精英.<a href="http://example.com/";目标=_blank";class=源">一些链接</a>Aenean commodo ligula eget dolor.Aenean massa </p><p>Aenean massa.Lorem ipsum dolor sat amet,consectetuer adipiscing elit.Aenean commodo ligula eget dolor.Aenean massa </p><p>Lorem ipsum dolor sat amet,consectetuer adipiscing 精英.Aenean commodo ligula eget dolor.Aenean massa </p><p>Consectetuer adipiscing 精英.Aenean commodo ligula eget dolor.Aenean massa</p></div>期望的输出:
洛伦ipsum dolor 坐 amet,consectetuer adipiscing 精英.Aenean commodoligula eget dolor.Aenean massa
Consectetuer adipiscing 精英.一些链接 Aenean commodo ligula eget dolor.埃尼马萨
埃尼安massa.Lorem ipsum dolor 坐 amet,consectetuer adipiscing 精英.埃涅斯commodo ligula eget dolor.Aenean massa
Lorem ipsum dolor satamet,consectetuer adipiscing 精英.Aenean commodo ligula eget dolor.Aenean massa
Consectetuer adipiscing 精英.Aenean commodoligula eget dolor.埃涅亚马萨
我尝试了 html2text
模块,但没有成功:
#!/usr/bin/env python导入 urllib2导入 html2text从 BeautifulSoup 导入 BeautifulSoup汤 = BeautifulSoup(urllib2.urlopen('http://example.com/page.html').read())txt = 汤.find('div', {'class' : 'body'})打印(html2text.html2text(txt))
txt
对象生成上面的 html 块.我想将其转换为文本并打印在屏幕上.
解决方案 soup.get_text()
输出你想要的:
from bs4 import BeautifulSoup汤 = BeautifulSoup(html)打印(汤.get_text())
输出:
Lorem ipsum dolor sat amet,consectetuer adipiscing 精英.Aenean commodo ligula eget dolor.埃涅斯马萨Consectetuer adipiscing 精英.Some Link Aenean commodo ligula eget dolor.埃涅斯马萨Aenean massa.Lorem ipsum dolor 坐 amet,consectetuer adipiscing 精英.Aenean commodo ligula eget dolor.埃涅斯马萨Lorem ipsum dolor 坐 amet,consectetuer adipiscing 精英.Aenean commodo ligula eget dolor.埃涅斯马萨Consectetuer adipiscing 精英.Aenean commodo ligula eget dolor.埃涅斯马萨
保持换行:
print(soup.get_text('
'))
为了与您的示例相同,您可以用两个换行符替换换行符:
soup.get_text().replace('
','
')
I am trying to convert an html block to text using Python.
Input:
<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>
Desired output:
I tried the html2text
module without much success:
#!/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))
The txt
object produces the html block above. I'd like to convert it to text and print it on the screen.
解决方案 soup.get_text()
outputs what you want:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print(soup.get_text())
output:
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
To keep newlines:
print(soup.get_text('
'))
To be identical to your example, you can replace a newline with two newlines:
soup.get_text().replace('
','
')
这篇关于使用 Python 将 html 转换为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-02 07:55