Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python beautifulsoup - getting input value

Tags:

I've got many table rows like this:

<tr>     <td>100</td>     <td>200</td>     <td><input type="radio" value="123599"></td> </tr> 

Iterate with:

table = BeautifulSoup(response).find(id="sometable") # Make soup.  for row in table.find_all("tr")[1:]: # Find rows.     cells = row.find_all("td") # Find cells.      points = int(cells[0].get_text())     gold = int(cells[1].get_text())     id = cells[2].input['value']      print id 

Error:

File "./script.py", line XX, in <module> id = cells[2].input['value'] TypeError: 'NoneType' object has no attribute '__getitem__' 

How can I get input value? I don't want to use regexp.

like image 552
Mateusz Jagiełło Avatar asked Jul 27 '12 15:07

Mateusz Jagiełło


1 Answers

soup = BeautifulSoup(html) try:     value = soup.find('input', {'id': 'xyz'}).get('value') except Exception as e:     print("Got unhandled exception %s" % str(e)) 
like image 103
sergi0 Avatar answered Oct 23 '22 19:10

sergi0