28

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?

JohnnyW
  • 483
  • 2
  • 7
  • 16
  • More options here: [Can beautiful soup output be sent to browser?](http://stackoverflow.com/questions/25706214/can-beautiful-soup-output-be-sent-to-browser/25706243#25706243) – alecxe Sep 08 '14 at 14:09

3 Answers3

41

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)
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Thanks. This is the simple solution I was looking for for now. – JohnnyW Jan 29 '14 at 17:31
  • [NamedTemporaryFile](https://docs.python.org/3.7/library/tempfile.html) with delete = False may be preferrable here, as it will (likely) get deleted eventually by the OS – Jay May 25 '19 at 08:05
  • 1
    @Jay, Thank you for the comment. Updated the answer to include an alternative using NamedTemporaryFile. – falsetru May 25 '19 at 10:25
  • 1
    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. – marvin Oct 02 '19 at 18:29
  • @marvin, Thank you for the comment. I updated the answer accordingly. – falsetru Oct 03 '19 at 01:31
1

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/.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
1

(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).

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • 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? – JohnnyW Jan 29 '14 at 17:41
  • @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). – Hugh Bothwell Jan 29 '14 at 18:09