Requirement:
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:
How to Create Python file
Generate a file with “python_file.py” (python file) name by using “nano command” as mentioned below to write python code in it
You can change the name of the file according to your choice.
How to 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:
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.
Press “Ctrl+s” to save the file and “Ctrl+x” to exit the file:
To check the working of code, run the below mentioned command to execute file “python_file.py”.
Above output shows that the connection is successful.
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:
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.
Run the below mentioned command to execute file “python_file.py”, to get the desired output:
How to open URL using “webbrowser” Module
“webbrowser” is one of the modules of python which is also used to open URLs or websites in python but it directs the link to the browser.
To open URL using “webbrowser” module, follow the steps mentioned below:
Step1: Importing “webbrowser” library
To open URL, firstly you need to import the “webbrowser” library in the “python_file.py” by below mentioned code line:
Step2: Opening URL using webbrowser module
To open the URL of specific website on browser using “webbrowser”, use the below mentioned syntax:
Insert your URL in place of “website_url” in above mentioned syntax.
To open the URL “https://linuxhint.com/” using “webbrowser” module, write the below mentioned code in “python_file.py” file:
get_url= webbrowser.open('https://linuxhint.com/')
The “get_url.getcode()” is used to get http Response Status Code , 200 means you have successfully opened the url.
To open the URL browser, execute the code written in “python_file.py” by below mentioned command:
Conclusion:
To get data from a website while programming, we need to open the URL. In this Article, I have discussed the methods to open URLs in python on Ubuntu (Linux System). Two ways are being discussed with examples, one is by importing the “urllib.request” module and other is by importing the “webbrowser” module in python. If you are a python programmer then after reading this article you will be able to open a URL in python with ease.
from https://ift.tt/3AdzecE
0 Comments