Web Scraping Using Python GUI

Platforuma India
4 min readApr 14, 2021

--

In this blog, we will learn about Web Scraping using Python (Tkinter Module).

What is Web Scraping in Python?

Web Scraping is a technique to extract data from web pages but in an automated way.

In this project, we can collect web data using any correct URL of a web site and find a particular word.

Now let us start a project using Python GUI for Web Scraping is :

Code for the project:

For the code of this project click on the link below:

https://github.com/Platforuma/medium_blog/blob/cf770c355439ec4d4175eaf71f07514df677460c/web_scraping.py

Define code working :-

from tkinter import *

import requests

from bs4 import BeautifulSoup

Tkinter is the name of the GUI library in Python. Creating a GUI application using Tkinter is an easy task.

BeautifulSoup?

BeautifulSoup is a Python library for pulling data out of HTML and XML files.

(For more information on BeautifulSoup Module, kindly refer to this documentation)

root = Tk()

root.title(“Web Scraper”)

root.geometry(“500x400”)

Tk class is used to create a root window. Here root object is the name of a root window. Tkinter provides many methods, one of them is the geometry() method.

(For more information on Geometry methods, kindly refer to this documentation)

def find():

if entry.get():

if entry_1.get():

url = entry.get()

r = requests.get(url)

htmlContent = r.content

soup = BeautifulSoup(htmlContent,”html.parser”)

abc = soup.find().get_text(entry_1.get())

file = open(entry_2.get(),”w”)

file.write(abc)

Now In the next line, we define a function and the function name is found.

Now, we are creating a URL variable where we hold the value from the entry object. Here we apply a get function on the entry object.

Now In this line, we define the HTML content variable to hold the value of the content.

Here in this line of code, we define the soup object to hold the value of the BeautifulSoup Method. We use the Beautiful Soup Method on the HTML content and also pass all content in the DOM form.

In this line of the code, we define the abc variable and here the abc variable holds the value of the soup. find function.

Here the next line of the code we define the file object for open any file and also we apply the write operation on the file.

label = Label(root,text=”Enter Correct URL”,font=(‘arial’,20),fg=”black”)

label.place(x=138,y=30)

entry = Entry(root, width=45)

entry.place(x=120, y=80)

label_1 = Label(root,text=”Find words”,font=(‘arial’,20),fg=”green”)

label_1.place(x=40,y=130)

entry_1 = Entry(root)

entry_1.place(x=200, y=142)

Now come to the Designing Part of the GUI window. Here we are using some Label, Entry Box and Button in a project.

(For more information on Label Method, kindly refer to the documentation)

In this line code, we assign the place of the label Label using x or y coordinate values.

(For more information on Place Method, kindly refer to the documentation)

Now we are going to work with the Entry Method. To define the entry object for the Entry Method on the GUI window and set the width of Entry. Then we assign the place of the entry Entry using x or y coordinates values.

Here again, we define one more label_1 Label on GUI window and write text on Label and set font and FG of the Label. Now again assign the place of the label_1 using coordinate values.Now again one more Entry Box defined with the object name entry_1 on the GUI window. Again we assign the place of the entry_1.

label_2 = Label(root,text=”Give name for save file”,font=(‘arial’,20),fg=”green”)

label_2.place(x=30,y=180)

entry_2 = Entry(root)

entry_2.place(x=320, y=190)

button = Button(root,width=10, text=”click”,command=find)

button.place(x=180, y=240)

root.mainloop()

In this line of the code, we define a label_2 Label on the GUI window and write text on Label and set font and FG of the Label. Then assign the place of the label_2. Here again, we define the entry_2 object. Again we assign the place of the entry_2 object.

Now come to the Button part of the project.

So let’s start with how Button works, Here we define a button object of the Button Method on the GUI window and give the find command on the Button for performing an action when Button clicks. Here we assign the place of the button object using coordinates values.

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

(For more information on File Handling, kindly refer to this documentation)

Here we complete a full GUI Project using Python Tkinter, requests , and BeautifulSoup module.

Refer to our more Blogs on Python from the link below:

https://medium.com/@platforuma

Visit Our Website:

Author: Priyanka Prasad

Editor: Riya Patidar, Devendra Patidar, Ashutosh Raghuwanshi

--

--

No responses yet