使用 BeautifulSoup 获取按钮标签内的数据
有时在使用 BeautifulSoup 时,您是否陷入必须在按钮标签中获取数据的问题?别担心。只需阅读这篇文章并了解如何做同样的事情。
例如,考虑这个具有按钮标签的简单页面源。
HTML
Apple
Python
# Python program to get data inside
# a button tag using BeautifulSoup
# Import the libraries BeautifulSoup
# and os
from bs4 import BeautifulSoup as bs
import os
# Remove the last segment of the path
base = os.path.dirname(os.path.abspath(__file__))
# Open the HTML in which you want to make
# changes
html = open(os.path.join(base, 'run.html'))
# Parse HTML file in Beautiful Soup
soup = bs(html, 'html.parser')
# Finding the location of button
btn = soup.find("button", {"id": "enjoy"})
# Obtaining the text stored inside button tag
btn_text = btn.text
# Obtaining the onclick link of button tag
btn_onclick = btn['onclick']
# Printing the values
print(btn_text)
print(btn_onclick)
在 HTML 代码中创建按钮后,您可以使用以下方法获取按钮标签内的文本:
btn_text=btn.text
print(btn_text)
此外,您可以使用以下方法在按钮标签内找到按钮的onclick 链接:
btn_onclick=btn['onclick']
print(btn_onclick)
获取按钮内文本的步骤
步骤 1:首先,导入库 Beautiful Soup 和 os.
from bs4 import BeautifulSoup as bs
import os
第 2 步:现在,通过输入您当前在其中工作的Python文件的名称,删除路径的最后一段。
base=os.path.dirname(os.path.abspath(‘#Name of Python file in which you are currently working))
第 3 步:然后,打开要从中读取值的 HTML 文件。
html=open(os.path.join(base, ‘#Name of HTML file from which you wish to read value’))
第 4 步:此外,解析 Beautiful Soup 中的 HTML 文件
soup=bs(html, 'html.parser')
第 5 步:接下来,找到要获取其数据的按钮。
btn=soup.find("button", {"id":"#Id name of the button"})
第 6 步:现在,要获取 HTML 中按钮标记内存储的文本,请使用:
btn_text=btn.text
步骤7:进一步,为了找到button标签内的onclick链接,你可以编写如下代码:
btn_onclick=btn['onclick']
第八步:最后,打印第六步和第七步得到的button标签的text和onclick链接。
print(btn_text)
print(btn_onclick)
下面是完整的实现:
Python
# Python program to get data inside
# a button tag using BeautifulSoup
# Import the libraries BeautifulSoup
# and os
from bs4 import BeautifulSoup as bs
import os
# Remove the last segment of the path
base = os.path.dirname(os.path.abspath(__file__))
# Open the HTML in which you want to make
# changes
html = open(os.path.join(base, 'run.html'))
# Parse HTML file in Beautiful Soup
soup = bs(html, 'html.parser')
# Finding the location of button
btn = soup.find("button", {"id": "enjoy"})
# Obtaining the text stored inside button tag
btn_text = btn.text
# Obtaining the onclick link of button tag
btn_onclick = btn['onclick']
# Printing the values
print(btn_text)
print(btn_onclick)
输出: