如何获得BeautifulSoup一个子元素的HTML重新presentation? [英] How to get HTML representation of a child element in BeautifulSoup?
本文介绍了如何获得BeautifulSoup一个子元素的HTML重新presentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我的HTML是:
< HTML><身体GT;<跨度>这是我的文字< / SPAN>< /身体GT;< / HTML>
我如何得到一个字符串再发生了什么里面含有presentation,即:
<跨度>这是我的文字< / SPAN>
解决方案
要获得元素的HTML重新presentation只需使用内置的 STR
函数:
汤= BeautifulSoup(< HTML><身体GT;<跨度>这是我的文字< / SPAN>< /身体GT;< / HTML>中)
跨度= soup.find('跨')
STR(跨度)#输出'<跨度>这是我的文字< / SPAN>'
Assuming my HTML is:
<html><body><span>This is my text</span></body></html>
How do I get a string representation of what's contained inside , i.e:
<span>This is my text</span>
解决方案
To get the html representation of an element just use the built in str
function:
soup = BeautifulSoup("<html><body><span>This is my text</span></body></html>")
span = soup.find('span')
str(span) # Outputs '<span>This is my text</span>'
这篇关于如何获得BeautifulSoup一个子元素的HTML重新presentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文