📜  使用 BeautifulSoup 单击按钮后获得价值

📅  最后修改于: 2022-05-13 01:55:32.994000             🧑  作者: Mango

使用 BeautifulSoup 单击按钮后获得价值

Python的BeautifulSoup 库除了从 HTML 或 XML 文件中提取数据外,还有助于搜索、修改和导航解析树。单击按钮后,您是否无法从小部件获取值?别担心。只需仔细阅读文章即可了解使用 BeautifulSoup 单击按钮后获取值的过程。

循序渐进的方法:

  • 首先,导入库 BeautifulSoup、os 和 Tkinter。
  • 现在,通过输入您当前在其中工作的Python文件的名称来删除路径的最后一段。
  • 然后,打开要从中读取值的 HTML 文件。
  • 此外,解析Beautiful Soup中的HTML文件
  • 接下来,在找到要从中获取值的小部件后获取文本。
  • 此外,创建一个应用程序,您可以在其中单击按钮
  • 为您的应用程序提供标题和几何图形。
  • 稍后,创建一个在单击按钮时执行的任何名称的函数。您可以提供任何函数名称。在这种情况下,我们假设函数名是 func。在函数内部,获取按钮点击后想要获取值的文件。接下来,在按钮单击后将值写入您希望获得的文件中
  • 在应用程序中构建按钮,单击该按钮时会给出结果
  • 此外,显示在上一步中创建的按钮。
  • 最后,制作用于在屏幕上显示 GUI 应用程序的循环。

执行:

考虑以下 HTML 源代码。

HTML


 
   My First Heading
 
 
   
         Fruits      
  • Apple
  •      
  • Banana
  •      
  • Mango
  •    
  


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()


假设您想在单击“单击此处!”按钮后获取txt 文件“text_file”中的值“Mango”,那么您可以编写以下代码。

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()

输出: