Getting value after button click with BeautifulSoup
The library, BeautifulSoup in Python apart from extracting data out of HTML or XML files, helps in searching, modifying, and navigating the parse tree. Are you not able to obtain the value from a widget after the button click? Don’t worry. Just read the article thoroughly to know the procedure of obtaining the value after button click with BeautifulSoup.
Step-by-step Approach:
- First, import the libraries BeautifulSoup, os, and Tkinter.
from bs4 import BeautifulSoup as bs
from tkinter import *
import os
- Now, remove the last segment of the path by entering the name of the Python file in which you are currently working.
base=os.path.dirname(os.path.abspath(‘#Name of Python file in which you are currently working))
- Then, open the HTML file from which you want to read the value.
html=open(os.path.join(base, ‘#Name of HTML file from which you wish to read value’))
- Moreover, parse the HTML file in Beautiful Soup
soup=bs(html, ‘html.parser’)
- Next, obtain the text after finding the widget from which you wish to obtain value.
value=soup.find(“#Name of widget”, {“id”:”#Id name of the widget”}).text
- Further, create an app in which you have an option to click on the button
app=Tk()
- Give the title and geometry to your app.
app.title(“#Title of the app”)
app.geometry(‘#Geometry you wish to give to app’)
- Later on, create a function of any name which gets executed when the button is clicked. You can give any function name. In this case, we are supposing the function name to be func. Inside the function, obtain the file in which you want to obtain the value after the button click. Next, write the value in the file you wish to get after the button click
def func():
with open(‘#Name of text file in which you wish to write value’, “w”, encoding=’utf-8′) as f_output:
f_output.write(value)
- Construct the button in the app which when clicked gives result
b1=Button(app, text=’#Text you want to give to button’, command=func)
- Moreover, display the button created in the previous step.
b1.grid(padx=#Padding from x-axis, pady=#Padding from y-axis)
- Finally, make the loop for displaying the GUI app on the screen.
app.mainloop( )
Implementation:
Consider the following HTML source code.
HTML
<!DOCTYPE html>
< html >
< head >
My First Heading
</ head >
< body >
< ul id = "list" >
Fruits
< li >Apple</ li >
< li >Banana</ li >
< li id = "here" >Mango</ li >
</ ul >
</ body >
</ html >
|
Let us consider you want to obtain the value ‘Mango’ in the txt file ‘text_file’ after the button click ‘Click here!’, then you can write the following code.
Python
from bs4 import BeautifulSoup as bs
import os
from tkinter import *
base = os.path.dirname(os.path.abspath( 'gfg3.py' ))
html = open (os.path.join(base, 'gfg.html' ))
soup = bs(html, 'html.parser' )
value = soup.find( "li" , { "id" : "here" }).text
app = Tk()
app.title( "Vinayak App" )
app.geometry( '600x400' )
def apple():
with open ( 'text_file.txt' , "w" , encoding = 'utf-8' ) as f_output:
f_output.write(value)
b1 = Button(app, text = 'Click Here!' , command = apple)
b1.grid(padx = 250 , pady = 150 )
app.mainloop()
|
Output:
Last Updated :
26 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...