What is urllib?
urllib is a Python module that can be used for opening URLs. It defines functions and classes to help in URL actions.
With Python you can also access and retrieve data from the internet like XML, HTML, JSON, etc. You can also use Python to work with this data directly. In this tutorial we are going to see how we can retrieve data from the web. For example, here we used a guru99 video URL, and we are going to access this video URL using Python as well as print HTML file of this URL
Python is an interpreted language; it has different libraries to perform various functions. A Uniform Resource Locator (URL) is actually a web address to open a specific site. Sometimes while working in python we need to fetch data from a website, for this we have to open the url of a specific website. So, to open a URL in python we need to import the specified module and perform some steps to open that URL. In this article we will discuss how to open a URL in Python using “urllib.request” and “webbrowser” modules on Ubuntu (Linux OS) through a defined procedure.
Any installed version of python (python3 is preinstalled on Ubuntu latest version)
Follow the any of the procedure explained below to open url in python:
Method 1:Open URL using “urllib.request” Module
The “urllib.request” is one of the modules of python that allows opening urls in python.
Step1: Importing “urllib.request” library
To open URL in python you firstly you need to import the “urllib.request”, insert below mentioned import code line before starting your code in the newly created “python_file.py” file:
Step2: Opening URL using urllib.request function
To open the URL of specific website using urllib.request, use the below mentioned syntax:
website URL: Insert the URL of the website which you want to fetch.
To open URL “ https://www.google.com/ “ , write the below mentioned code in your python file:
import urllib.request get_url= urllib.request.urlopen('https://www.google.com/') print("Response Status: "+ str(get_url.getcode()) )
HTTP has defined response status codes; “get_url.getcode” is used to get that code. The digit “200” means your connection is successful, if it is “404” then that means the url is not recognized. Visit this source to learn about other status codes.
The “get_url” It is the variable that gets the data from the specified url and “print” is used to print the output.
Let’s check another example; we can also retrieve HTML code from the URL of any website. Run the below mentioned code to open url https://www.youtube.com/ and print its html code:
import urllib.request get_url= urllib.request.urlopen('https://www.youtube.com/') print("Response Status: "+ str(get_url.getcode())) print(get_url.read())
The “get_url.getcode()” is used to get http Response Status Code and “get_url.read()” is used to retrieve the html file of a website.
In the Python package, we have 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, and then we have to call some of its functions (declared and defined below) with our required inputs. Hence this module will then open up the browser we want and will get the page we want.
The methods present in this module are described below:
|S No.||Syntax of Method||Description|
|1||webbrowser.open(url, new = 0, autoraise = true)||This is the main method where the web browser with the passed URL is opened and is displayed to the user. If the parameter “new” is 0 then URL is opened in the same browser and if it is 1 then URL is opened in another browser and if it’s 2, then the page is opened in another tab.|
|2||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 one.|
|3||webbrowser.open_new_tab(url)||Opens new tab of passpage URL passed in the browser which is currently active.|
|4||webbrowser.get(using=None)||This command is used to get the object code for the web browser we want to use. In simple words,, we could use this command to get the code of the web browser (stored in python) and then we could use that code to open that particular web browser. We passes the name of the web browser which we want to use as a string.|
|5||webbrowser.register(name, 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 beginning, none of the browsers is registered and only the default one is called each time. Hence we had to register them manually.|
Now we are going to use these methods to see how we could open browsers with our passed URLs.
Below is the implementation:
We have used an URL = https://pystackcode.com/, for this experiment. Our python script is stored in a file named file.py. Below is the code which we had written and implemented in it.
Method 2: Basic example of Open Web Browser in Python Script
# first import the module import webbrowser # then make a url variable url = "https://pystackcode.com" # then call the default open method described above webbrowser.open(url)
Hence as a result the website got opened in the default web browser. As in mine, it is opened in Microsoft Edge as shown below
Method 3: Specify the browser
# first import the module import webbrowser # then make a url variable url = "https://pystackcode.com" # then call the get method to select the code # for new browser and call open method # described above webbrowser.get('chrome').open(url) # results in error since chrome is not registered initially.
Run the command in the prompt by typing “file.py” and it will give the results. If we will try to open the browser of our choice without registering it for the first time then we will get the below-given type of response as an output.
Hence below given is the code which we modified so that it now registers the browsers and then opens that URL in it.
Method 4: Register the new browser
# first import the module import webbrowser # then make a url variable url = "https://pystackcode.com" # getting path chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe" # First registers the new browser webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path)) # after registering we can open it by getting its code. webbrowser.get('chrome').open(url)
he output of using this command in the prompt is the same as given above but only the method is different.
Hence after executing it will open the chrome web browser.
There is one other method also for opening the browser using webbrowser in python. In this method, we don’t have to write the whole script and interpret it to open the browsers. We could just use python shell to execute a single command (given below) to open up the browser (default one) with a specified URL.
The shell command is given as:-
python -m webbrowser -t “https://pystackcode.com”
Hence we saw two different methods and explained to them how to open a web browser using python scripts.
Source Below Stackoverflow:
I new to Stackoverflow so still learning how to write proper answers.,See: Python: generic webbrowser.get().open() for chrome.exe does not work, Meta Stack Overflow ,Stack Overflow en español
You can call get() with the path to Chrome. Below is an example – replace chrome_path with the correct path for your platform.
import webbrowser url = 'http://docs.python.org/' # MacOS chrome_path = 'open -a /Applications/Google\ Chrome.app %s' # Windows # chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' # Linux # chrome_path = '/usr/bin/google-chrome %s' webbrowser.get(chrome_path).open(url)
In the case of Windows, the path uses a UNIX-style path, so make the backslash into forward slashes.
webbrowser.get(“C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s”).open(“http://google.com”)
import webbrowser new = 2 # open in a new tab, if possible # open a public URL, in this case, the webbrowser docs url = "http://docs.python.org/library/webbrowser.html" webbrowser.get(using = 'google-chrome').open(url, new = new)
Please check this:
import webbrowser chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' webbrowser.get(chrome_path).open('http://docs.python.org/')
worked for me to open new tab on google-chrome:
import webbrowser webbrowser.open_new_tab("http://www.google.com")
python - m webbrowser - t "https://www.python.org"
Register the browser type name using webbrowser.register(). Also provide the browser executable file path.,Get the controller object for the browser using webbrowser.get() and Open URL using open().,To open URL in a browser, we will use webbrowser python module.,In this tutorial of Python Examples, we learned how to open a URL in Chrome browser.
import webbrowser url = 'https://pythonexamples.org' webbrowser.register('chrome', None, webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe")) webbrowser.get('chrome').open(url)
import webbrowser url = 'https://pythonexamples.org' webbrowser.register('chrome', None, webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe")) webbrowser.get('chrome').open_new(url)