PYTHON URLLIB MODULE

Platforuma India
5 min readMay 4, 2021

--

Let us first comprehend what URLLIB Module is?

Urllib Module is the URL handling module for Python. It is used to fetch the URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.

In this project, we will import the urllib module in which we will send a request to the server to open the URL, and after getting responses from the server we store all that responses in a text file.

For more information on URLLIB Module, kindly refer to this documentation (https://www.geeksforgeeks.org/)

In other term urllib is a package that collects several modules for working with URLs:

  • urllib.request for opening and reading.
  • urllib.parse for parsing URLs
  • urllib.error for the exceptions raised
  • urllib.robotparser for parsing robot.txt files

Now let us start a project using Python GUI for URLLIB Module:

Code for the project:

https://github.com/Platforuma/medium_blog/blob/341001e1cadeeeb9cfd17ed1bc337eeb3f29c703/Urllib.py

Now let us deep-dive into a better understanding of the code line by line.

Define code working:-

from tkinter import *
from urllib.request import Request, urlopen

Tk class is used to create a root window. The root is the name of a root window. Here Tk function provides a window of GUI as well as provides so many features like setting the title, set the geometry of the GUI window.

Here in this line of the code, we Import Request and urlopen Module from the urllib Module. The requests module allows you to send HTTP requests using Python.

root = Tk()
root.title("URLLIB MODULE")
root.geometry("500x250")

We already learned how title methods work on the Tkinter module.

In these steps, we are giving the title of the GUI root window. The title is defined about the project in one line what we do in the project.

Tkinter provides many methods, one of them is the geometry() method. This method is used to set the dimensions of the Tkinter window and it is used to set the position of the main window on the user desktop.

In our previous Python GUI Project, we already worked on the Tk class, title and geometry methods.

Here in this line, we define the Function. The function name is Open_url. We define the global variable, where the name of the variable is text. Variable is used to hold some data or value in programming.

def Open_url():
global text
text.set("")
if entry_window.get():
req = Request(entry_window.get(), headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
file = open(entry_window2.get(),"w")
file.write(str(webpage))
text.set("Open URL Successfully …")

Again we define the Set Function on the text variable with the null value. We define the if condition. In this line of the code, we define the req variable to hold the value of Request.

Here in this line of code, we define the webpage variable. The webpage variable holds the value of URL which is got from the req Variable in reading mode. Now in this line of code, we define the file object to hold the file and we open the file using the open method in write mode. Now in this line of the code, we again use the set function to set the value of the text variable.

text = StringVar()
label = Label(root,text="Enter any URL :")
label.place(x=95, y=10)
entry_window = Entry(root,justify=CENTER, width=50, borderwidth=4)
entry_window.place(x=95,y=30)
label = Label(root,text="Enter file name for save response :")
label.place(x=95, y=60)
entry_window2 = Entry(root,justify=CENTER, width=50, borderwidth=4)
entry_window2.place(x=95, y=80)

Now in this line of the code, we define the text variable. The type of text variable is StringVar, so means the text variable holds a string type value.

Now come to Designing part of the GUI project. Here we are using some Label, Entry Box and Button in a project. So let's start to comprehend the working.

Here in this line of the code we define the label object of the Label Method on the GUI Window and set text on the window. We assign the place of the label Object with the value of x or y coordinates.

Entry Box, here we define the entry_window object of the Entry Method on the GUI Window and set justify, width and border width of the window. We assign the place of the entry_window object with the value of x or y coordinates. Here we again define the label object. Now assign the place of the label object.

Alternatively, we define the entry_window2 object of the Entry Method on the GUI Window. Assigns place of the entry_window2 object with the value of x or y coordinates.

label = Label(root,text="Status of Response :")
label.place(x=95, y=110)
entry_window1 = Entry(root,justify=CENTER, width=50, borderwidth=4, textvariable=text)
entry_window1.place(x=95,y=130,height=30)

Now again we define the label object of the Label Method on the GUI Window and set text on the window and assigns the place. Entry_window1 object and assigns the place.

Now come to the main part of the project is Button because the project shows all functionality work that is added on the button.

btn_check = Button(root,text="Open URL", command=Open_url)
btn_check.place(x=220,y=180)
root.mainloop()

In the previous Python GUI Project, we comprehend how buttons work.

In this line of code, we define the btn_check object of the Button Method on the GUI window and set text and font fg, bg, and also give the open_url command on the btn_check for adding the functionality on the function when the button is pressed. Here we assign the place of the btn_check object with the value of x or y coordinates.

Coming to the last line of code of the project is the mainloop function that provides an infinite loop.

mainloop() is an infinite loop used to run the application, using this function window is not closed as long as.

Here we ended a full GUI Project using the Python Tkinter, Request and Urlopen Module...

--

--

No responses yet