如何从一个美丽的汤对象 HTML

我有以下 bs4对象清单:

>>> listing
<div class="listingHeader">
<h2>
....




>>> type(listing)
<class 'bs4.element.Tag'>

我想提取原始的 html 作为一个字符串。我试过:

>>> a = listing.contents
>>> type(a)
<type 'list'>

所以这没用,我该怎么做?

94848 次浏览

Just get the string representation:

html_content = str(listing)

This is a non-prettified version.

If you want a prettified one, use prettify() method:

html_content = listing.prettify()