本文介绍了我该如何转动< br>和< p>换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个内含< p> 和< br> 标签的HTML。事后,我会去掉HTML来清理标签。我怎样才能把它们变成换行符?
Let's say I have an HTML with <p> and <br> tags inside. Aftewards, I'm going to strip the HTML to clean up the tags. How can I turn them into line breaks?
我使用Python的库,如果有帮助的话。
I'm using Python's BeautifulSoup library, if that helps at all.
推荐答案
没有一些细节,很难请确定这正是你想要的,但是这应该给你这个想法......它假设你的b标签被包装在p元素中。
Without some specifics, it's hard to be sure this does exactly what you want, but this should give you the idea... it assumes your b tags are wrapped inside p elements.
from BeautifulSoup import BeautifulSoup import types def replace_with_newlines(element): text = '' for elem in element.recursiveChildGenerator(): if isinstance(elem, types.StringTypes): text += elem.strip() elif elem.name == 'br': text += '\n' return text page = """<html> <body> <p>America,<br> Now is the<br>time for all good men to come to the aid<br>of their country.</p> <p>pile on taxpayer debt<br></p> <p>Now is the<br>time for all good men to come to the aid<br>of their country.</p> </body> </html> """ soup = BeautifulSoup(page) lines = soup.find("body") for line in lines.findAll('p'): line = replace_with_newlines(line) print line
运行此结果...
Running this results in...
(py26_default)[mpenning@Bucksnort ~]$ python thing.py America, Now is the time for all good men to come to the aid of their country. pile on taxpayer debt Now is the time for all good men to come to the aid of their country. (py26_default)[mpenning@Bucksnort ~]$
这篇关于我该如何转动< br>和< p>换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!