如何从美丽的汤对象中获取HTML

时间:2022-03-08 17:03:21

I have the following bs4 object listing:

我有以下bs4对象列表:

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


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

I want to extract the raw html as a string. I've tried:

我想将原始html提取为字符串。我试过了:

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

So this does not work. How can I do this?

所以这不起作用。我怎样才能做到这一点?

1 个解决方案

#1


52  

Just get the string representation:

只需获取字符串表示:

html_content = str(listing)

This is a non-prettified version.

这是一个非美化版本。

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

如果你想要一个美化的,请使用prettify()方法:

html_content = listing.prettify()

#1


52  

Just get the string representation:

只需获取字符串表示:

html_content = str(listing)

This is a non-prettified version.

这是一个非美化版本。

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

如果你想要一个美化的,请使用prettify()方法:

html_content = listing.prettify()