How to open Python http responses in a web browser?

by scrapecrow Dec 19, 2022

To view Python's HTTP responses in a web browser we can save the contents to a temporary file and open it in the default web browser using Python's webbrowser module:

import webbrowser
from tempfile import NamedTemporaryFile

# this can work with any response object of any http client like:
import requests
import httpx

def view_in_browser(response):
    """open httpx or requests Response object in default browser"""
    # first - save content to a temporary file:
    with NamedTemporaryFile("wb", delete=False, suffix=".html") as file:
        file.write(response.content)
    # open temporary file in a new browser tab as a web page
    webbrowser.open_new_tab(f"file://{file.name}")
    # - or new window
    # webbrowser.open_new(f"file://{file.name}")
    # - or current active tab
    # webbrowser.open(f"file://{file.name}")
    
# example use:
response = requests.get("http://scrapfly.io/")
response = httpx.get("http://scrapfly.io/")  
view_in_browser(response)

This is a great tool for developing web scrapers as it allows to easily visualize and debug the scraper process as well as to use the browser's developer tools.

Related Articles

Guide to Python requests POST method

Discover how to use Python's requests library for POST requests, including JSON, form data, and file uploads, along with response handling tips.

PYTHON
REQUESTS
HTTP
Guide to Python requests POST method

Guide to Python Requests Headers

Our guide to request headers for Python requests library. How to configure and what do they mean.

PYTHON
REQUESTS
HTTP
Guide to Python Requests Headers

How to Scrape Forms

Learn how to scrape forms through a step-by-step guide using HTTP clients and headless browsers.

HEADLESS-BROWSER
PYTHON
HTTPX
INTRO
NODEJS
How to Scrape Forms

How to Web Scrape with HTTPX and Python

Intro to using Python's httpx library for web scraping. Proxy and user agent rotation and common web scraping challenges, tips and tricks.

HTTPX
PYTHON
How to Web Scrape with HTTPX and Python

How to Scrape YouTube in 2025

Learn how to scrape YouTube, channel, video, and comment data using Python directly in JSON.

SCRAPEGUIDE
PYTHON
HIDDEN-API
How to Scrape YouTube in 2025

Guide to List Crawling: Everything You Need to Know

In-depth look at list crawling - how to extract valuable data from list-formatted content like tables, listicles and paginated pages.

CRAWLING
BEAUTIFULSOUP
PYTHON
Guide to List Crawling: Everything You Need to Know