How to open Python http responses in a web browser?

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:
    # open temporary file in a new browser tab as a web page
    # - or new window
    # webbrowser.open_new(f"file://{}")
    # - or current active tab
# example use:
response = requests.get("")
response = httpx.get("")  

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.

Question tagged: Python, requests, httpx

Related Posts

How to Scrape Forms

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

How to Build a Minimum Advertised Price (MAP) Monitoring Tool

Learn what minimum advertised price monitoring is and how to apply its concept using Python web scraping.

How to Scrape Reddit Posts, Subreddits and Profiles

In this article, we'll explore how to scrape Reddit. We'll extract various social data types from subreddits, posts, and user pages. All of which through plain HTTP requests without headless browser usage.