Skip to content
Related Articles

Related Articles

Improve Article
Getting value after button click with BeautifulSoup
  • Last Updated : 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.

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




# Python program to obtain value after button click
  
# Import the libraries BeautifulSoup, tkinter and os
from bs4 import BeautifulSoup as bs
import os
from tkinter import *
  
# Remove the last segment of the path
base = os.path.dirname(os.path.abspath('gfg3.py'))
  
# Open the HTML in which you want to make changes
html = open(os.path.join(base, 'gfg.html'))
  
# Parse HTML file in Beautiful Soup
soup = bs(html, 'html.parser')
  
# Find the value which you want to obtain after button click
value = soup.find("li", {"id": "here"}).text
  
# Construct the app for clicking of button
app = Tk()
  
# Give title to your GUI app
app.title("Vinayak App")
  
# Set dimensions for the app
app.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 app
b1 = Button(app, text='Click Here!', command=apple)
  
# Display the button created in previous step
b1.grid(padx=250, pady=150)
  
# Make the loop for displaying app
app.mainloop()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :