问题
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?
回答1:
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()
来源:https://stackoverflow.com/questions/25729589/how-to-get-html-from-a-beautiful-soup-object