I am trying to extract all the contents of the value tag in a div
. I am able to get the contents of the <div class>
, but I've been unable to get the contents of the values inside of that class:
<div class='styled-radio'>
<input type="radio" name="variant_id" id="variant_id_105589" value="105589"
class="js-change-quantity" data-count-on-hand="1" data-options-threshold="5"
/>
<label for="variant_id_105589">41</label>
<input type="radio" name="variant_id" id="variant_id_105591" value="105591"
class="js-change-quantity" data-count-on-hand="1" data-options-threshold="5"
/>
<label for="variant_id_105591">43</label>
</div>
Any ideas why?
for example if you want all value
of input
tag you can do this
from bs4 import BeautifulSoup
sdata = """
<div class='styled-radio'>
<input type="radio" name="variant_id" id="variant_id_105589" value="105589"
class="js-change-quantity" data-count-on-hand="1" data-options-threshold="5"
/>
<label for="variant_id_105589">41</label>
<input type="radio" name="variant_id" id="variant_id_105591" value="105591"
class="js-change-quantity" data-count-on-hand="1" data-options-threshold="5"
/>
<label for="variant_id_105591">43</label>
</div>
"""
soup = BeautifulSoup(sdata)
mydivs = soup.findAll('div', {'class': 'styled-radio'})
for div in mydivs:
# if you want to print a dict of all values use it
# for children in div.findAll():
# print(children.attrs)
# if you want to print a specific attr from a specific tag use it
for children in div.findAll('input'): # find all inputs from `div`
print(children['value']) # get `value` attrs
Output
105589
105591
Update @bobrobbob is right we can get attrs without findChildren()
and also without attrs
it also will work and output will be the same
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.