【问题标题】:How to fetch aria-label as text on basis of div id in Python using Beautiful or Selenium如何使用 Beautiful 或 Selenium 在 Python 中基于 div id 获取 aria-label 作为文本
【发布时间】:2021-10-29 00:30:16
【问题描述】:
<div id="i19" class="quantumWizTogglePapercheckboxEl appsMaterialWizTogglePapercheckboxCheckbox docssharedWizToggleLabeledControl freebirdThemedCheckbox freebirdThemedCheckboxDarkerDisabled freebirdMaterialWidgetsToggleLabeledCheckbox isCheckedNext isChecked" jscontroller="EcW08c" jsaction="keydown:I481le;dyRcpb:dyRcpb;click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue; focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd;" jsshadow="" jsname="FkQz1b" aria-label="Al-Baraka Bank Limited" data-answer-value="Al-Baraka Bank (Pakistan) Limited" role="checkbox" aria-checked="true" tabindex="0">

嗨,我正在尝试使用 div id i19 获取此 div Al-Baraka Bank Limited 中 aria-label 内的文本。基本上我点击一个复选框,我希望将文本(即 aria-label)保存到一个变量中,以便我可以在其他地方再次使用它。

【问题讨论】:

  • 到目前为止你尝试过什么?那里面临哪些问题和错误?

标签: python html selenium web-scraping beautifulsoup


【解决方案1】:

如果那个idunique,那么你可以通过get_attribute获取属性值。

aria_label = driver.find_element(By.ID, "i19").get_attribute('aria-label')
print(aria_label)

【讨论】:

    【解决方案2】:

    我把你的数据取为html,你可以根据id找到div,然后提取它的属性

    html="""<div id="i19" class="quantumWizTogglePapercheckboxEl appsMaterialWizTogglePapercheckboxCheckbox docssharedWizToggleLabeledControl freebirdThemedCheckbox freebirdThemedCheckboxDarkerDisabled freebirdMaterialWidgetsToggleLabeledCheckbox isCheckedNext isChecked" jscontroller="EcW08c" jsaction="keydown:I481le;dyRcpb:dyRcpb;click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue; focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd;" jsshadow="" jsname="FkQz1b" aria-label="Al-Baraka Bank Limited" data-answer-value="Al-Baraka Bank (Pakistan) Limited" role="checkbox" aria-checked="true" tabindex="0">
    """
    from bs4 import BeautifulSoup
    soup=BeautifulSoup(html,"lxml")
    soup.find("div",attrs={"id":"i19"})['aria-label']
    

    输出:

    'Al-Baraka Bank Limited'
    

    或者:

    它还以字典形式返回,因此您可以根据键查找以从中提取值!

    soup.find("div",attrs={"id":"i19"}).attrs
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 2017-07-29
      • 2022-01-25
      • 1970-01-01
      • 2023-01-17
      相关资源
      最近更新 更多