Launch HTML code in browser (that is generated by BeautifulSoup) straight from Python

15,114

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:

  1. Create a web file directory and save your .html file in it.

  2. Open a command-line window and do

    cd /my/web/directory
    python -m http.server 8000
    
  3. 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/.

Share:
15,114

Related videos on Youtube

JohnnyW
Author by

JohnnyW

Updated on July 14, 2022

Comments

  • JohnnyW
    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?

  • reptilicus
    reptilicus over 10 years
    or just SimpleHTTPServer?
  • falsetru
    falsetru over 10 years
    @reptilicus, FYI, no SimpleHTTPServer in Python 3.x, but http.server.
  • JohnnyW
    JohnnyW over 10 years
    Thanks. This is the simple solution I was looking for for now.
  • JohnnyW
    JohnnyW over 10 years
    Thanks. 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
    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
    Jay almost 5 years
    NamedTemporaryFile with delete = False may be preferrable here, as it will (likely) get deleted eventually by the OS
  • falsetru
    falsetru almost 5 years
    @Jay, Thank you for the comment. Updated the answer to include an alternative using NamedTemporaryFile.
  • marvin
    marvin over 4 years
    I 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
    falsetru over 4 years
    @marvin, Thank you for the comment. I updated the answer accordingly.