Python beautifulsoup - 获取输入值 [英] Python beautifulsoup - getting input value

查看:23
本文介绍了Python beautifulsoup - 获取输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多这样的表格行:

I've got many table rows like this:

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

迭代:

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

错误:

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.

推荐答案

soup = BeautifulSoup(html)
try:
    value = soup.find('input', {'id': 'xyz'}).get('value')
except Exception as e:
    print("Got unhandled exception %s" % str(e))

这篇关于Python beautifulsoup - 获取输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆