1

I'm trying to output the HTML from a webpage, and do it in a formatted way. For example, I want to make this: enter image description here

Look like this: enter image description here

My code is:

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

I do not wish to print this to a file. I wish to display on screen with coloring and indentation.

  • 1
    Jupyter doesn't print strings with colors unless you actually want to render HTML itself – OneCricketeer Dec 31 '21 at 17:10
  • 3
    You can use beautifulsoup.[Check out](https://stackoverflow.com/questions/6150108/how-to-pretty-print-html-to-a-file-with-indentation) – RSale Dec 31 '21 at 17:12
  • Welcome aboard. This should probably be closed as a duplicate. This doesn’t mean your question is invalid or lacking in any way, only that substantially the same thing was asked before. And you *are* getting the answers from that dupe ( I second BeautifulSoup’s prettify). Mind you: the answers dont colorize, so perhaps a separate colorize question is warranted (no idea if that’s been asked before) except that Jupyter/html/bash/ what have you all handle colors differently. – JL Peyret Dec 31 '21 at 18:45

1 Answers1

1

I think this pip package will help you https://pythonhosted.org/html5print/

Install:

pip install html5print

Example usage

 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))
Mahdi mehrabi
  • 1,486
  • 2
  • 18
  • 21