html - BeautifulSoup4 get input 'value' throws an error with good code? -


print [(element['name'], element['value']) element in soup.find_all('input')] 

i copied code value of input , throws error:

 file "messager.py", line 116, in main    print [(element['name'], element['value']) element in soup.find_all('input')]  file "c:\python27\lib\site-packages\bs4\element.py", line 905, in __getitem__    return self.attrs[key] keyerror: 'value' 

if provide name, works. tried several other versions, still gives me same value key error. seems doesn't return value.

either way, here's returns if search 'inputs' (example of 1 result):

<input id="token" name="data[token][key]" type="hidden" value="152a1f397548bfecbb09581670734bf0403bdf63"/> 

so finds 'inputs' can't values.

any ideas on why happens?

probably, there input elements without value attribute being set.

you can let find_all() know want inputs value , nameattributes set:

soup.find_all("input", {"name": true, "value": true}) 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -