Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Open a URL in Your Browser From a Python Script?

#1
How to Open a URL in Your Browser From a Python Script?

<div>
<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;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<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’t forget to run <code>import webbrowser</code> first. But you don’t have to install the module because it’s already in Python’s standard library.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="1195" height="663" src="https://blog.finxter.com/wp-content/uploads/2022/10/open_url.gif" alt="" class="wp-image-854841"/></figure>
</div>
<h2>Example</h2>
<p>Here’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>
<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
webbrowser.open('https://finxter.com/')</pre>
<p>A new browser tab with your default browser (Chrome, Edge, Safari, Brave — 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>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...00x174.png 300w, https://blog.finxter.com/wp-content/uplo...68x446.png 768w, https://blog.finxter.com/wp-content/uplo...36x892.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-295.png 1906w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<h2>About the Webbrowser Module</h2>
<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>
<p>You can also run the module from your command line or terminal by using the following command:</p>
<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>
<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>
<h2>Webbrowser open()</h2>
<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>
<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>
<p>The <code>new</code> argument allows you to control the browser window:</p>
<ul>
<li>If you set <code>new=0</code> (default), you open the URL in the same browser window.</li>
<li>If you set <code>new=1</code>, you open a new browser window. </li>
<li>If you set <code>new=2</code>, you open a new browser tab.</li>
</ul>
<p>The <code>autoraise</code> argument allows you to raise the window (default behavior). </p>
<h2>Webbrowser Open in New Tab</h2>
<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>
<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
my_url = 'https://finxter.com'
webbrowser.open_new_tab(my_url)</pre>
<h2>Select the Webbrowser</h2>
<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>
<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
webbrowser.get("chrome").open("https://finxter.com")</pre>
<p>Here are the supported browser types:</p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Type Name</th>
<th>Class Name</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>'mozilla'</code></td>
<td><code>Mozilla('mozilla')</code></td>
</tr>
<tr>
<td><code>'firefox'</code></td>
<td><code>Mozilla('mozilla')</code></td>
</tr>
<tr>
<td><code>'netscape'</code></td>
<td><code>Mozilla('netscape')</code></td>
</tr>
<tr>
<td><code>'galeon'</code></td>
<td><code>Galeon('galeon')</code></td>
</tr>
<tr>
<td><code>'epiphany'</code></td>
<td><code>Galeon('epiphany')</code></td>
</tr>
<tr>
<td><code>'skipstone'</code></td>
<td><code>BackgroundBrowser('skipstone')</code></td>
</tr>
<tr>
<td><code>'kfmclient'</code></td>
<td><code>Konqueror()</code></td>
</tr>
<tr>
<td><code>'konqueror'</code></td>
<td><code>Konqueror()</code></td>
</tr>
<tr>
<td><code>'kfm'</code></td>
<td><code>Konqueror()</code></td>
</tr>
<tr>
<td><code>'mosaic'</code></td>
<td><code>BackgroundBrowser('mosaic')</code></td>
</tr>
<tr>
<td><code>'opera'</code></td>
<td><code>Opera()</code></td>
</tr>
<tr>
<td><code>'grail'</code></td>
<td><code>Grail()</code></td>
</tr>
<tr>
<td><code>'links'</code></td>
<td><code>GenericBrowser('links')</code></td>
</tr>
<tr>
<td><code>'elinks'</code></td>
<td><code>Elinks('elinks')</code></td>
</tr>
<tr>
<td><code>'lynx'</code></td>
<td><code>GenericBrowser('lynx')</code></td>
</tr>
<tr>
<td><code>'w3m'</code></td>
<td><code>GenericBrowser('w3m')</code></td>
</tr>
<tr>
<td><code>'windows-default'</code></td>
<td><code>WindowsDefault</code></td>
</tr>
<tr>
<td><code>'macosx'</code></td>
<td><code>MacOSXOSAScript('default')</code></td>
</tr>
<tr>
<td><code>'safari'</code></td>
<td><code>MacOSXOSAScript('safari')</code></td>
</tr>
<tr>
<td><code>'google-chrome'</code></td>
<td><code>Chrome('google-chrome')</code></td>
</tr>
<tr>
<td><code>'chrome'</code></td>
<td><code>Chrome('chrome')</code></td>
</tr>
<tr>
<td><code>'chromium'</code></td>
<td><code>Chromium('chromium')</code></td>
</tr>
<tr>
<td><code>'chromium-browser'</code></td>
<td><code>Chromium('chromium-browser')</code></td>
</tr>
</tbody>
</table>
</figure>
<h2>Thanks for Reading <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2764.png" alt="❤" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </h2>
<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 src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
</div>


https://www.sickgaming.net/blog/2022/10/...on-script/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016