【问题标题】:Format a string containing HTML to display with indenting, hierarchy and colors [duplicate]格式化包含 HTML 的字符串以显示缩进、层次结构和颜色 [重复]
【发布时间】:2021-12-31 16:58:25
【问题描述】:

我正在尝试从网页输出 HTML,并以格式化的方式进行。例如,我想做这个:

看起来像这样:

我的代码是:

url = "https://dex.raydium.io/#/market/MTc1macY8G2v1MubFxDp4W8cooaSBUZvc2KqaCNwhQE"
page  = urlopen(url)
html_bytes = page.read()
html_help = html_bytes.decode("utf-8")
print(html_help)

我不希望将其打印到文件中。我希望在屏幕上显示颜色和缩进。

【问题讨论】:

  • Jupyter 不会打印带有颜色的字符串,除非你真的想渲染 HTML 本身
  • 你可以用beautifulsoup.Check out
  • 欢迎加入。这可能应该作为副本关闭。这并不意味着您的问题在任何方面都是无效或缺乏的,只是之前问过的问题基本相同。你正在从那个骗子那里得到答案(我第二个 BeautifulSoup 的美化)。请注意:答案不会着色,因此可能需要一个单独的着色问题(不知道之前是否有人问过),除了 Jupyter/html/bash/ 你们处理颜色的方式不同。

标签: python


【解决方案1】:

我认为这个 pip 包会帮助你 https://pythonhosted.org/html5print/

安装:

pip install html5print

示例用法

 from html5print import CSSBeautifier
 html = '''<html><body>
   <style>
     .para { margin: 10px 20px; }
 <!-- This is what the function is dealing with-->
 p { color: red; font-style: normal; }
   </style>
 </body></html>'''
print(CSSBeautifier.beautifyTextInHTML(html))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2012-01-31
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多