python - 怎么查看Beautiful Soup的prettify(encoding, formatter="minimal")
巴扎黑
巴扎黑 2017-04-18 10:27:16
[Python讨论组]
soup=bs(html)
html2 = soup.prettify('utf-8', formatter='minimal')

prettify() 方法的第二个参数 formatter 到底有几个合法的取值呢?

我们只知道有 minimal, 还有什么呢?我们都不知道.
怎么找到这些参数的值呢?
这是 python 最不好的一个地方,方法的说明里不写明白,使用者怎么寻找这些特定的取值呢?

巴扎黑
巴扎黑

全部回复(2)
迷茫

在官方的 doc 裡面就有完整的說明了:

Output formatters

  • The default is formatter="minimal". Strings will only be processed enough to ensure that Beautiful Soup generates valid HTML/XML

  • If you pass in formatter="html", Beautiful Soup will convert Unicode characters to HTML entities whenever possible

  • If you pass in formatter=None, Beautiful Soup will not modify strings at all on output. This is the fastest option, but it may lead to Beautiful Soup generating invalid HTML/XML

  • Finally, if you pass in a function for formatter, Beautiful Soup will call that function once for every string and attribute value in the document. You can do whatever you want in this function.

至於要找 code:

In [1]: import bs4

In [2]: bs4.BeautifulSoup.prettify.__code__
Out[2]: <code object prettify at 0x103f7f5d0, file "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/bs4/element.py", line 1198>

我回答過的問題: Python-QA

伊谢尔伦

看代码...

    HTML_FORMATTERS = {
        "html" : HTMLAwareEntitySubstitution.substitute_html,
        "minimal" : HTMLAwareEntitySubstitution.substitute_xml,
        None : None
        }

    XML_FORMATTERS = {
        "html" : EntitySubstitution.substitute_html,
        "minimal" : EntitySubstitution.substitute_xml,
        None : None
        }

可以见 https://imgur.com/gallery/VkNUv

不知道怎么显示不出来这个图片

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号