soup.get_text() outputs what you want:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print(soup.get_text())
output:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Consectetuer adipiscing elit. Some Link Aenean commodo ligula eget dolor. Aenean massa
Aenean massa.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
To keep newlines:
print(soup.get_text('\n'))
To be identical to your example, you can replace a newline with two newlines:
soup.get_text().replace('\n','\n\n')
Answer from root on Stack OverflowHow can I convert Python into HTML
Converting html to text with Python - Stack Overflow
Convert Python code into anything else: HTML, CSS, React, Javascript
How to convert HTML into Python code?
Why convert text to HTML in Python?
Can I convert Python to HTML automatically?
Should I rewrite from scratch or use a converter for Python to HTML?
Videos
I'm learning the Python and the HTML programming languages right now and I just learned how to make a hello world function. I want to now print this function on my website.
However, putting the python function inside my HTML program didn't work. Is there some sort of process to convert my python program into an HTML program so I can show it on my website?
Also, my website only seems to work on my computer. I was trying to show a friend my website, but it wouldn't load on his computer. My website URL is "C:\Users\Nathan\Website\website.html"
soup.get_text() outputs what you want:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print(soup.get_text())
output:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Consectetuer adipiscing elit. Some Link Aenean commodo ligula eget dolor. Aenean massa
Aenean massa.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa
To keep newlines:
print(soup.get_text('\n'))
To be identical to your example, you can replace a newline with two newlines:
soup.get_text().replace('\n','\n\n')
It's possible using python standard html.parser:
from html.parser import HTMLParser
class HTMLFilter(HTMLParser):
text = ""
def handle_data(self, data):
self.text += data
f = HTMLFilter()
f.feed(data)
print(f.text)
» pip install html2text
Hii, a friend and I made a website using HTML for our final project but unfortunately, we have to submit it using Flask. This means that we have to rewrite the code using Python, is there any way to convert our original HTML code into Python without rewriting it?
Do:
<pre>
import os
print os.system("dir")
def test():
a = 5 + 6
print a
test()
</pre>
pre tag displays text with fixed-width font, and it preserves both spaces and line breaks.
I am a bit late to the party, but you can use Jupyter Notebook. Paste your code in a cell and export the notebook as HTML.
Just change your code to include <pre> and </pre> tags to ensure that your text stays formatted the way you have formatted it in your original text file.
contents = open"C:\\Users\\Suleiman JK\\Desktop\\Static_hash\\test","r")
with open("suleiman.html", "w") as e:
for lines in contents.readlines():
e.write("<pre>" + lines + "</pre> <br>\n")
This is HTML -- use BeautifulSoup
from bs4 import BeautifulSoup
soup = BeautifulSoup()
body = soup.new_tag('body')
soup.insert(0, body)
table = soup.new_tag('table')
body.insert(0, table)
with open('path/to/input/file.txt') as infile:
for line in infile:
row = soup.new_tag('tr')
col1, col2 = line.split()
for coltext in (col2, col1): # important that you reverse order
col = soup.new_tag('td')
col.string = coltext
row.insert(0, col)
table.insert(len(table.contents), row)
with open('path/to/output/file.html', 'w') as outfile:
outfile.write(soup.prettify())
If you were to put...
I am Python Man . I am 13 years old. This time I got 1st position in my
class.
Into a .html file then load it, it'd work fine. If you want to format it properly with tags directly from the python file, you could change your print() line to, for example...
print("<p>I am",name,". I am",age,"years old. This time I got",position,"position
in my class.</p>")
There is several ways, and depends on what you really want to achieve, so I'll explain couple of options you have:
you can add any HTML tags you want in the string "as it's already HTML that doesn't have any tags to format it" but you will have to escape special characters that may break your HTML using built-in Module " html"
you can also use third-party modules such as this which can generate HTML for you, and it's usage described briefly in the link.
The choice is simple if you have HTML knowledge and want to customize your HTML go for the first option, if not or you want easy solution go for the module.