HTML alignment using Python

Asked

Viewed 95 times

1

I need to line up a html code, in some parts it comes totally "messy", example:

<li><a href="dsadas">dsadaads</a></li><li><a href="dsadas">dsadaads</a></li><li><a href="dsadas">dsadaads</a></li>

HTML comes in one line, and I want it to be organized like this:

<li><a href="dsadas">dsadaads</a></li>
<li><a href="dsadas">dsadaads</a></li>
<li><a href="dsadas">dsadaads</a></li>

An example would be this website, he organizes everything. Now the question is: How can I align the entire site using the PYTHON 2.7. I noticed that there is a library of the site jsbeautifier.

I downloaded it and tried to use, but I did not understand very well the functioning.

My code downloads the HTML from my site, in which there is a long list. Then the file comes as . HTML

JSBEAUTIFER

datafile = file('meusitehtml.html')
import jsbeautifier
res = jsbeautifier.beautify(datafile)
res = jsbeautifier.beautify_file('some_file.js')

I don’t quite understand how I can use jsbeautifier.

-> I need an example of how I can use jsbeautifier?

  • In VS Code you should try to make it tranquil with the Beautify https://marketplace.visualstudio.com/items?itemName=HookyQR.beautify#Overview

  • With Google Developer Tool you can do this right in Chrome, if you want me to put the answer with some images teaching.

2 answers

1

You can use the Beautiful Soup

from bs4 import BeautifulSoup as bs

content = '<li><a href="dsadas">dsadaads</a></li><li><a href="dsadas">dsadaads</a></li><li><a href="dsadas">dsadaads</a></li>'
soup = bs(content, "html5lib")
prettyHTML = soup.prettify()

Printing the result

print(prettyHTML)

<html>
 <head>
 </head>
 <body>
  <li>
   <a href="dsadas">
    dsadaads
   </a>
  </li>
  <li>
   <a href="dsadas">
    dsadaads
   </a>
  </li>
  <li>
   <a href="dsadas">
    dsadaads
   </a>
  </li>
 </body>
</html>

I hope it helps

0


This is simple to do using lxml library.

http://lxml.de

Follow an example of code:

from lxml import etree, html
documento = html.fromstring('<li><a href="dsadas">dsadaads</a></li><li><a href="dsadas">dsadaads</a></li><li><a href="dsadas">dsadaads</a></li>')
html_formatado = etree.tostring(documento, pretty_print=True).decode('utf8')
print(html_formatado)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.