derd1199
derd1199

Reputation: 31

Python Beautiful soup, select secont value

I have a Html Code what looks like

 <input type="hidden" value="" id="productBsId" />
.
.
.
.
 <input type="hidden" value="61980" id="productBsId" />

When I try sizefund = soup.find('input', {'id': 'productBsId'}).get('value')

It prints "" instead of "61980". So it selects the first value and prints it. How can I select the second?

Upvotes: 0

Views: 79

Answers (1)

Wondercricket
Wondercricket

Reputation: 7882

You are very close, you just need to handle elements where the value attribute is empty

sizefund = soup.find('input', {'id': 'productBsId', 'value': lambda o: o != ''}).get('value')

or you can use findAll and take the second element

sizefund = soup.findAll('input', {'id': 'productBsId'})[1].get('value')

Upvotes: 1

Related Questions