开发者

What is the best way to get the values of a radio button set with lxml?

开发者 https://www.devze.com 2023-04-10 02:28 出处:网络
For example I have the following radio buttons: <input type=\"radio\" name=\"hand\"id=\'hand_left\' checked value=\"L\">

For example I have the following radio buttons:

<input type="radio" name="hand"  id='hand_left' checked value="L"> 
  <label for='hand_left'>Left</label>

<input type="radio" name="hand" id='hand_right' value="R"> 
  <label for='hand_right'>Right</label>

And have the following parsing code:

import lxml.html as lh

doc=lh.parse(response) # response is the response of 
                       # http post that returns the above html

for el in doc.iter('input'):
   if el.name == 'hand':
     print el

That prints out two InputElements, but lxml only returns a value for the开发者_开发技巧 checked one and returns None for the unchecked one (as per the lxml docs). I'd like to get each of the potential values regardless of which one is checked.

<InputElement 8715f8c name='hand' type='radio'>
<InputElement 8715f8f name='hand' type='radio'>


You could use el.attrib['value'] instead of el.value

0

精彩评论

暂无评论...
验证码 换一张
取 消