Python BS4 - 如何查找特定类中的所有属性

时间:2018-06-10 09:57:02

标签: python beautifulsoup

我正在尝试在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>

任何想法为什么?

1 个答案:

答案 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()它也可以工作和输出将是相同的