Python的beautifulsoup - 让输入值 [英] Python beautifulsoup - getting input value

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

问题描述

我有很多表行是这样的:

 < TR>
    &所述; TD> 100℃; / TD>
    &所述; TD> 200℃; / TD>
    < TD><输入类型=无线电VALUE =123599>< / TD>
< / TR>

与迭代:

 表= BeautifulSoup(响应).find(ID =sometable)#使汤。在table.find_all行(TR)[1:]:#查找行。
    细胞= row.find_all(TD)#查找细胞。    点= INT(细胞[0] .get_text())
    金= INT(细胞[1] .get_text())
    ID =细胞[2]。输入['值']    打印ID

错误:

 文件./script.pyXX线,上述<&模块GT;
ID =细胞[2]。输入['值']
类型错误:'NoneType'对象有没有属性'__getitem__

我怎样才能输入值?我不希望使用正则表达式。


解决方案

 汤= BeautifulSoup(HTML)
尝试:
    值= soup.find('输入',{'ID':'某某'})得到('值')
除:
    通过

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:
    pass

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

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