我正在尝试在div
中提取值标记的所有内容。
我能够获取<div 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>
任何想法为什么?
答案 0 :(得分:1)
例如,如果您想要所有TextBox
value
标记,则可以执行此操作
input
<强>输出强>
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
<强>更新强>
@bobrobbob是对的我们可以在没有105589
105591
的情况下获得attrs而且没有findChildren()
它也可以工作和输出将是相同的