[英]Python Beautiful soup, select secont value
I have a Html Code what looks like 我有一个HTML代码,看起来像什么
<input type="hidden" value="" id="productBsId" />
.
.
.
.
<input type="hidden" value="61980" id="productBsId" />
When I try sizefund = soup.find('input', {'id': 'productBsId'}).get('value')
当我尝试
sizefund = soup.find('input', {'id': 'productBsId'}).get('value')
It prints "" instead of "61980". 它打印“”而不是“ 61980”。 So it selects the first value and prints it.
因此,它将选择第一个值并进行打印。 How can I select the second?
如何选择第二个?
You are very close, you just need to handle elements where the value
attribute is empty 您非常接近,您只需要处理
value
属性为空的元素
sizefund = soup.find('input', {'id': 'productBsId', 'value': lambda o: o != ''}).get('value')
or you can use findAll
and take the second element 或者您可以使用
findAll
并接受第二个元素
sizefund = soup.findAll('input', {'id': 'productBsId'})[1].get('value')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.