[英]Python BS4 - How to find all attributes in a specific class
I am trying to extract all the contents of the value tag in a div
. 我试图提取
div
value标记的所有内容。 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>
的内容,但是无法获取该类内部值的内容:
<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 例如,如果您想要
input
标签的所有value
,则可以执行此操作
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 更新 @bobrobbob是正确的,我们可以在没有
findChildren()
情况下获得attrs,并且在没有attrs
情况下也可以正常工作,并且输出将相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.