I have several websites with which I need to interact with the checkboxes.

Some of the html looks like this:

      <input type="checkbox" />
      <input type="checkbox" checked="checked" />

And some has an attribute "value." I need a way to return the state and to interact with a checkbox in both situation. But the below code doesn't work because it looks for the attribute value. Is there a way to handle checkboxes in general?

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))
  • Your request appears to be blocked by the website: str(website) is <html><head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html. You probably need to include a fake User-Agent. – SuperStormer 2 days ago
  • Thank you, I am updating the original post with one that isn't blocking it. Sorry. – TheNomadicAspie 2 days ago
  • The returned don't have value attribute. Some have checked attribute. You can't interact with them as you are using requests. – QHarr yesterday
  • You can do a more generic select('[type=checkbox]') then check the attrs of each returned element – QHarr yesterday
  • @QHarr no need for CSS selectors, you can do that with just find and type=checkbox is only standardized for <input> anyways. – SuperStormer yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.