1. 问题描述
from pyquery import PyQuery as pq
html="<h3></h3>"
a=pq(html)
print(a.html())
//会输出 <h3/>
2.解决办法
from pyquery import PyQuery as pq
html="<h3></h3>"
a=pq(html)
print(a.html(method='html'))
//会输出 <h3></h3>
注意,以下不能起到同样的效果
from pyquery import PyQuery as pq
html="<h3></h3>"
a=pq(html)
print(a.outer_html())
//会输出 <h3/>
print(a.outerHtml())
//会输出 <h3/>
print(a.outer_html(method='html'))
//会报错
print(a.outerHtml(method='html'))
//会报错
本文介绍了使用PyQuery处理HTML时遇到的问题及解决方法。当处理包含自闭合标签的HTML字符串时,PyQuery默认将其输出为自闭合形式。文章提供了如何通过指定方法参数来保留原始HTML结构的方法,并对比了不同方法的效果。

2075

被折叠的 条评论
为什么被折叠?



