Python 如何从漂亮的汤对象中获取 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25729589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:33:04 来源:igfitidea点击:
How to get HTML from a beautiful soup object
提问by user61629
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?
所以这是行不通的。我怎样才能做到这一点?
采纳答案by alecxe
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()

