简体   繁体   中英

How to get HTML from a beautiful soup object

I have the following bs4 object listing:

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


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

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

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

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM