Getting value after button click with BeautifulSoup - GeeksforGeeks

26 Mar, 2021
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.
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( )
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
# Python program to obtain value after button click  # Import the libraries BeautifulSoup, tkinter and osfrom bs4 import BeautifulSoup as bsimport osfrom tkinter import *  # Remove the last segment of the pathbase = os.path.dirname(os.path.abspath('gfg3.py'))  # Open the HTML in which you want to make changeshtml = open(os.path.join(base, 'gfg.html'))  # Parse HTML file in Beautiful Soupsoup = bs(html, 'html.parser')  # Find the value which you want to obtain after button clickvalue = soup.find("li", {"id": "here"}).text  # Construct the app for clicking of buttonapp = Tk()  # Give title to your GUI appapp.title("Vinayak App")  # Set dimensions for the appapp.geometry('600x400')    def apple():      # Open the file in which you want to obtain the value    with open('text_file.txt', "w", encoding='utf-8') as f_output:          # Writing the value in the file        f_output.write(value)    # Construct the button in your appb1 = Button(app, text='Click Here!', command=apple)  # Display the button created in previous stepb1.grid(padx=250, pady=150)  # Make the loop for displaying appapp.mainloop()
Output:
Picked
Python BeautifulSoup
Python bs4-Exercises
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Python OOPs Concepts
Python | Get unique values from a list
Check if element exists in list in Python
Python Classes and Objects
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Python | Pandas dataframe.groupby()
Create a directory in Python