{"id":129359,"date":"2022-10-31T13:39:07","date_gmt":"2022-10-31T13:39:07","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=854814"},"modified":"2022-10-31T13:39:07","modified_gmt":"2022-10-31T13:39:07","slug":"how-to-open-a-url-in-your-browser-from-a-python-script","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/31\/how-to-open-a-url-in-your-browser-from-a-python-script\/","title":{"rendered":"How to Open a URL in Your Browser From a Python Script?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;854814&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\">To open a URL in your standard browser (Win, macOS, Linux) from your Python script, e.g., call <code>webbrowser.open('https:\/\/google.com')<\/code> to open Google. Don&#8217;t forget to run <code>import webbrowser<\/code> first. But you don&#8217;t have to install the module because it&#8217;s already in Python&#8217;s standard library.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1195\" height=\"663\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/open_url.gif\" alt=\"\" class=\"wp-image-854841\"\/><\/figure>\n<\/div>\n<h2>Example<\/h2>\n<p>Here&#8217;s an example Python script that opens the URL <code><a rel=\"noreferrer noopener\" href=\"https:\/\/finxter.com\" data-type=\"URL\" data-id=\"https:\/\/finxter.com\" target=\"_blank\">'https:\/\/finxter.com'<\/a><\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import webbrowser\nwebbrowser.open('https:\/\/finxter.com\/')<\/pre>\n<p>A new browser tab with your default browser (Chrome, Edge, Safari, Brave &#8212; whatever you set up as standard browser in your OS settings) opens, initialized with the URL provided as a string argument of the <code>webbrowser.open()<\/code> function:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"595\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-295-1024x595.png\" alt=\"\" class=\"wp-image-854840\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-295-1024x595.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-295-300x174.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-295-768x446.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-295-1536x892.png 1536w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-295.png 1906w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<h2>About the Webbrowser Module<\/h2>\n<p>The <code><a href=\"https:\/\/docs.python.org\/3\/library\/webbrowser.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/webbrowser.html\" target=\"_blank\" rel=\"noreferrer noopener\">webbrowser<\/a><\/code> module is already part of the Python Standard Library, so you can import it without needing to install it first. <\/p>\n<p>You can also run the module from your command line or terminal by using the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python -m webbrowser -t \"https:\/\/finxter.com\"<\/pre>\n<p>Good to know if you ever want to open a URL from your operating system command line or terminal (Windows, macOS, Linux, Ubuntu) because the fact that you use Python makes it portable and operating system independent!<\/p>\n<h2>Webbrowser open()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">You can specify additional arguments to get more control over which tab is opened by means of the new argument of the <code>webbrowser.open()<\/code> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">webbrowser.open(url, new=0, autoraise=True)<\/pre>\n<p>The <code>new<\/code> argument allows you to control the browser window:<\/p>\n<ul>\n<li>If you set <code>new=0<\/code> (default), you open the URL in the same browser window.<\/li>\n<li>If you set <code>new=1<\/code>, you open a new browser window. <\/li>\n<li>If you set <code>new=2<\/code>, you open a new browser tab.<\/li>\n<\/ul>\n<p>The <code>autoraise<\/code> argument allows you to raise the window (default behavior). <\/p>\n<h2>Webbrowser Open in New Tab<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">A short way of opening a given URL in a new tab from your Python script is to call <code>webbrowser.open_new_tab()<\/code> and pass your URL string as a single argument.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import webbrowser\nmy_url = 'https:\/\/finxter.com'\nwebbrowser.open_new_tab(my_url)<\/pre>\n<h2>Select the Webbrowser<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">You can also return a controller object for a given browser by calling <code>webbrowser.get()<\/code> and passing the browser type into it. Now, you can call the <code>open()<\/code> or <code>open_new_tab()<\/code> methods on this controller object to open the URL in your desired web browser.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import webbrowser\nwebbrowser.get(\"chrome\").open(\"https:\/\/finxter.com\")<\/pre>\n<p>Here are the supported browser types:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Type Name<\/th>\n<th>Class Name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>'mozilla'<\/code><\/td>\n<td><code>Mozilla('mozilla')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'firefox'<\/code><\/td>\n<td><code>Mozilla('mozilla')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'netscape'<\/code><\/td>\n<td><code>Mozilla('netscape')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'galeon'<\/code><\/td>\n<td><code>Galeon('galeon')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'epiphany'<\/code><\/td>\n<td><code>Galeon('epiphany')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'skipstone'<\/code><\/td>\n<td><code>BackgroundBrowser('skipstone')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'kfmclient'<\/code><\/td>\n<td><code>Konqueror()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'konqueror'<\/code><\/td>\n<td><code>Konqueror()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'kfm'<\/code><\/td>\n<td><code>Konqueror()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'mosaic'<\/code><\/td>\n<td><code>BackgroundBrowser('mosaic')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'opera'<\/code><\/td>\n<td><code>Opera()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'grail'<\/code><\/td>\n<td><code>Grail()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'links'<\/code><\/td>\n<td><code>GenericBrowser('links')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'elinks'<\/code><\/td>\n<td><code>Elinks('elinks')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'lynx'<\/code><\/td>\n<td><code>GenericBrowser('lynx')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'w3m'<\/code><\/td>\n<td><code>GenericBrowser('w3m')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'windows-default'<\/code><\/td>\n<td><code>WindowsDefault<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'macosx'<\/code><\/td>\n<td><code>MacOSXOSAScript('default')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'safari'<\/code><\/td>\n<td><code>MacOSXOSAScript('safari')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'google-chrome'<\/code><\/td>\n<td><code>Chrome('google-chrome')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'chrome'<\/code><\/td>\n<td><code>Chrome('chrome')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'chromium'<\/code><\/td>\n<td><code>Chromium('chromium')<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>'chromium-browser'<\/code><\/td>\n<td><code>Chromium('chromium-browser')<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2>Thanks for Reading <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2764.png\" alt=\"\u2764\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/h2>\n<p>To keep learning, feel free to check out our <a href=\"https:\/\/blog.finxter.com\/email-academy\/\" data-type=\"page\" data-id=\"12278\" target=\"_blank\" rel=\"noreferrer noopener\">email academy<\/a> and download our free Python cheat sheets. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) To open a URL in your standard browser (Win, macOS, Linux) from your Python script, e.g., call webbrowser.open(&#8216;https:\/\/google.com&#8217;) to open Google. Don&#8217;t forget to run import webbrowser first. But you don&#8217;t have to install the module because it&#8217;s already in Python&#8217;s standard library. Example Here&#8217;s an example Python script that opens [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-129359","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=129359"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129359\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=129359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=129359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=129359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}