Launch HTML code in browser (that is generated by BeautifulSoup) straight from Python
Solution 1
Using webbrowser.open
:
import os
import webbrowser
html = '<html> ... generated html string ...</html>'
path = os.path.abspath('temp.html')
url = 'file://' + path
with open(path, 'w') as f:
f.write(html)
webbrowser.open(url)
Alternative using NamedTemporaryFile
(to make the file eventually deleted by OS):
import tempfile
import webbrowser
html = '<html> ... generated html string ...</html>'
with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
url = 'file://' + f.name
f.write(html)
webbrowser.open(url)
Solution 2
(this grew enough I figured I should split it off as a separate answer:)
As @reptilicus points out, you can use the built-in http.server module as follows:
Create a web file directory and save your .html file in it.
-
Open a command-line window and do
cd /my/web/directory python -m http.server 8000
Point your browser at
http://127.0.0.1:8000
This only works for static files; it will not run your script and return the results (as Flask does).
Solution 3
Use Flask to turn your code into a local web application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def scrape_and_reformat():
# call your scraping code here
return '<html><body> ... generated html string ... </body></html>'
if __name__ == '__main__':
app.run()
Run the script, and point your browser at http://127.0.0.1:5000/
.
Related videos on Youtube
JohnnyW
Updated on July 14, 2022Comments
-
JohnnyW almost 2 years
I have used BeautifulSoup for Python 3.3 to successfully pull desired info from a web page. I have also used BeautifulSoup to generate new HTML code to display this info. Currently, my Python program prints out the HTML code, which I then have to copy, paste, and save as an HTML file, then from there, I can test it in a browser.
So my question is this, is there a way in Python to launch the HTML code generated by BeautifulSoup in a web browser so that I don't have to go through the copy and paste method I use now?
-
alecxe over 9 yearsMore options here: Can beautiful soup output be sent to browser?
-
-
reptilicus over 10 yearsor just SimpleHTTPServer?
-
falsetru over 10 years@reptilicus, FYI, no SimpleHTTPServer in Python 3.x, but
http.server
. -
JohnnyW over 10 yearsThanks. This is the simple solution I was looking for for now.
-
JohnnyW over 10 yearsThanks. This is the first time that I've tried doing anything with HTML in Python so this http:server is a little over my head right now. Would this be a useful module to learn if I intend on eventually putting my code online?
-
BasedRebel over 10 years@user3108789: It's really not difficult; it literally takes about 15 seconds to do. Follow the steps above and try it! To work on 'actual hosting' sites, I would use Flask instead (as above); once you have a bit of experience, try Django (but that's a much larger environment, with a bigger learning curve. Flask is much easier to get started in).
-
Jay almost 5 yearsNamedTemporaryFile with delete = False may be preferrable here, as it will (likely) get deleted eventually by the OS
-
falsetru almost 5 years@Jay, Thank you for the comment. Updated the answer to include an alternative using NamedTemporaryFile.
-
marvin over 4 yearsI needed to use
with tempfile.NamedTemporaryFile('w', delete=False,suffix=".html") as f:
to get it to render the html properly in the browser. Otherwise I just saw the raw html string in my browser. -
falsetru over 4 years@marvin, Thank you for the comment. I updated the answer accordingly.