简体   繁体   English

Python beautifulsoup - 获取输入值

[英]Python beautifulsoup - getting input value

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.我不想使用正则表达式。

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

你想在单元格内找到 <input> 元素,所以你应该像这样在单元格上使用 find/find_all :

cells[2].find('input')['value']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM