Python 3 Read a Url in Browser
In this article nosotros will be discussing some of the methods that can be used to open a web browser (of our selection) and visit the URL we specified, using python scripts.
In the Python bundle, nosotros take a module named webbrowser, which includes a lot of methods that we can use to open the required URL in any specified browser we want. For that, we just have to import this module to our script file, then we have to call some of its functions (declared and defined beneath) with our required inputs. Hence this module will and so open up the browser nosotros want and will get the page nosotros want.
The methods present in this module are described beneath:
S No. | Syntax of Method | Description |
---|---|---|
i | webbrowser.open(url, new = 0, autoraise = true) | This is the main method where the spider web browser with the passed URL is opened and is displayed to the user. If the parameter "new" is 0 and then URL is opened in the same browser and if information technology is 1 then URL is opened in another browser and if it'south ii, so the folio is opened in another tab. |
two | webbrowser.open_new(url) | URL passed is opened in the a new browser if it is possible to do that, else it is opened in default 1. |
3 | webbrowser.open_new_tab(url) | Opens new tab of passpage URL passed in the browser which is currently agile. |
four | webbrowser.get(using=None) | This command is used to get the object code for the spider web browser we want to utilise. In simple words,, nosotros could use this control to get the code of the web browser (stored in python) and then nosotros could use that code to open that detail web browser. We passes the name of the spider web browser which we want to use as a cord. |
5 | webbrowser.register(proper noun, constructor, instance=None, preferred=False) | This method is used to register the name of the favorite browser in the Python environment if its code was not registered previously. Actually, at the get-go, none of the browsers is registered and only the default i is called each time. Hence we had to register them manually. |
Now we are going to employ these methods to meet how nosotros could open browsers with our passed URLs.
Below is the implementation:
We take used an URL = https://www.geeksforgeeks.org, for this experiment. Our python script is stored in a file named geeks.py. Below is the lawmaking which nosotros had written and implemented in it.
Instance i: Basic example of Open Spider web Browser in Python Script
Python3
Hence as a result the website got opened in the default web browser. As in mine, information technology is opened in Microsoft Border as shown below:
Example 2: Specify the browser
Python3
Run the command in the prompt by typing "python geeks.py" and it will give the results. If nosotros volition attempt to open up the browser of our option without registering information technology for the first time then we will become the below-given type of response as an output.
Hence below given is the code which we modified so that information technology at present registers the browsers and and then opens that URL in it.
Example 3: Register the new browser
Python3
import
webbrowser
chrome_path
=
r
"C:\Plan Files\Google\Chrome\Application\chrome.exe"
webbrowser.annals(
'chrome'
,
None
,
webbrowser.BackgroundBrowser(chrome_path))
webbrowser.get(
'chrome'
).
open
(url)
The output of using this control in the prompt is the same as given above simply merely the method is different.
Hence subsequently executing information technology will open the chrome web browser.
There is i other method too for opening the browser using webbrowser in python. In this method, nosotros don't have to write the whole script and interpret it to open the browsers. We could just use python crush to execute a single command (given below) to open the browser (default i) with a specified URL.
The shell control is given as:-
python -m webbrowser -t "https://www.geeksforgeeks.org"
Hence we saw two different methods and explained to them how to open a web browser using python scripts.
Source: https://www.geeksforgeeks.org/python-script-to-open-a-web-browser/
0 Response to "Python 3 Read a Url in Browser"
Post a Comment