首页 > 解决方案 > Python 的美丽汤 - 返回所有复选框?

问题描述

我有几个网站需要与复选框进行交互。

一些 html 看起来像这样:

      <input type="checkbox" />
      <span>Red</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" checked="checked" />
      <span>Yellow</span>

还有一些具有属性“值”。我需要一种方法来返回状态并在这两种情况下与复选框进行交互。但是下面的代码不起作用,因为它会查找属性值。一般有没有办法处理复选框?

def GetCheckboxes(website):
    checkboxes = website.find('input', {"type": "checkbox"})["value"]
    return checkboxes

def GetWebsite(url):
    result = requests.get(url)
    src = result.content
    soup = BeautifulSoup(src, 'lxml')
    return soup

website = GetWebsite('https://materializecss.com/checkboxes.html')
print (GetCheckboxes(website))

标签: pythonpython-3.xbeautifulsoup

解决方案


推荐阅读