Webbrowser Package Basic

PythonPythonBeginner
Practice Now

Introduction

The webbrowser module in Python provides a simple interface to open web browsers, display HTML documents, and navigate the web. This practical lab will walk you through the basics of using the webbrowser package, from opening a URL in a new tab to executing a Google search directly from the Python console.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/function_definition -.-> lab-8689{{"`Webbrowser Package Basic`"}} python/importing_modules -.-> lab-8689{{"`Webbrowser Package Basic`"}} python/standard_libraries -.-> lab-8689{{"`Webbrowser Package Basic`"}} python/file_opening_closing -.-> lab-8689{{"`Webbrowser Package Basic`"}} python/build_in_functions -.-> lab-8689{{"`Webbrowser Package Basic`"}} end

Opening a URL in a New Tab

The webbrowser module makes it easy to open a URL in a new browser tab. Let's start by importing the webbrowser module and calling the open_new_tab() function to open a URL:

Open the Python Shell

Open the Python shell by typing the following command in the terminal.

python3

Import the web browser module and use the open_new_tab() function to open the URL.

import webbrowser
url = "https://www.google.com"
webbrowser.open_new_tab(url)

When you run this code, a new browser tab should open and navigate to Google's homepage.

Opening a URL in the Default Browser

If you want to open a URL in the user's default browser, you can use the open() function instead of open_new_tab():

webbrowser.open(url)

Displaying Local HTML Files

The webbrowser module can also be used to display HTML files. A simple HTML file named "example.html" is provided here.

We can use the open_new_tab() function to display this HTML file in a new browser tab:

file_path = "/home/labex/project/example.html"
webbrowser.open_new_tab(file_path)

When you run this code, a new browser tab should open and display the contents of example.html.

Searching Google from Python

The webbrowser module can even be used to execute a Google search directly from the Python console. Let's create a function that takes a search query as an argument and uses the webbrowser module to execute a Google search:

def google_search(query):
    url = "https://www.google.com/search?q=" + query
    webbrowser.open_new_tab(url)

Now we can call the google_search() function with a search query:

google_search("python web scraping")

When you run this code, a new browser tab should open and display the Google search results for "python web scraping".

Summary

In this lab, you learned the basics of using Python's webbrowser package to interact with the web. You learned how to open a URL in a new browser tab, display local HTML files, and even execute a Google search directly from the Python console. The webbrowser module is a powerful tool for automating web-based tasks and integrating Python scripts with the web.

Other Python Tutorials you may like