Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用漂亮的汤剥离HTML时保留空间 从美化组导入美化组 html=“_Python_Html_Beautifulsoup - Fatal编程技术网

Python 用漂亮的汤剥离HTML时保留空间 从美化组导入美化组 html=“

Python 用漂亮的汤剥离HTML时保留空间 从美化组导入美化组 html=“,python,html,beautifulsoup,Python,Html,Beautifulsoup,第1段。单词合并。第2段引述1引述2” 打印html soup=BeautifulSoup(html) 打印u“”。加入(soup.findAll(text=True)) 本规范的出处是“第1段文字注释。第2段引用1引用2” 我不希望第一段的最后一个字和第二段的第一个字合并。 例如,“第1段词语合并。第2段引文1引文2”。 这可以通过使用BeautifulSoup库来实现吗 只需用空格将各个部分连接起来: from BeautifulSoup import BeautifulSoup htm

第1段。单词

合并。第2段引述1引述2

” 打印html soup=BeautifulSoup(html) 打印u“”。加入(soup.findAll(text=True)) 本规范的出处是“第1段文字注释。第2段引用1引用2”

我不希望第一段的最后一个字和第二段的第一个字合并。 例如,“第1段词语合并。第2段引文1引文2”。
这可以通过使用BeautifulSoup库来实现吗

只需用空格将各个部分连接起来:

from BeautifulSoup import BeautifulSoup

html = "<html><p>Para 1. Words</p><p>Merge. Para 2<blockquote>Quote 1<blockquote>Quote 2</p></html>"
print html
soup = BeautifulSoup(html)
print u''.join(soup.findAll(text=True))
如果您在版本4.x中使用:

print u' '.join(soup.findAll(text=True))
from bs4 import BeautifulSoup
...
...
soup.get_text(" ")