from bs4 import BeautifulSoup as Soup
import json
b = '''
<section class="warr-section is-expand" id="upgradeWarranty"><span class="show-differences differences-button notDisplay"></span><span class="hide-differences differences-button notDisplay"></span><div class="stackableSection stackable-selected" groupcode="Depot Warranty" priority="2"><div class="sectionContent sectionContentDisplay"><div class="merchContainer hasImg upgrande-warranty-merchcontainer" style="height: 517px;"><div class="merchExpandArrow"><i class="fa fa-angle-down fa-2x merch-expand-arrow" isexpand="true"></i></div><div class="merchImg"><img src="/medias/depot.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDIzMTB8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGYyL2gzMi8xMDU0OTkxMzcxNDcxOC5wbmd8ZTIwNGYwMjQxZTY1YjIzNjRmNGRlZDFmZTgzMmY2YTgwYThhZGNjZDg2MGFhMWFjYzkzZjU3MjAyNzMzZGZmNw" style="height: 55px;"></div><div class="stackableHeader stackabel-header-pos upgrade-warranty-stackable-header" style="height: 64px;"><span class="merchImg-for-mobile notDisplay"><img src="/medias/depot.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDIzMTB8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGYyL2gzMi8xMDU0OTkxMzcxNDcxOC5wbmd8ZTIwNGYwMjQxZTY1YjIzNjRmNGRlZDFmZTgzMmY2YTgwYThhZGNjZDg2MGFhMWFjYzkzZjU3MjAyNzMzZGZmNw"></span><span class="good-tag-for-mobile notDisplay">GOOD</span><span class="pos_group_title">Depot Support</span><span class="pos_group_info_icon" attr-code="Depot Warranty"><img src="/_ui/desktop/common/css/images/pos_group_icon.png"></span><span class="startAt-for-mobile notDisplay">One Year</span><span class="expand-icon-for-mobile notDisplay fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-up fa-stack-1x"></i></span></div><div class="good-better-best-tag hastag selected-group" style="height: 44px;"><span class="good-better-best-text">GOOD</span></div><div class="merchText select-warranty-merchtext"><p style="height: 40px;" class="InsidecontentText" row-index="0"><span><i class="fa fa-check"> </i></span><span>Parts & Labor</span></p> <p class="InsidecontentText" row-index="1" style="height: 57px;"><span><i class="fa fa-check"> </i></span><span>Basic Phone Support for hardware issues</span></p> <p class="InsidecontentText" row-index="2" style="height: 77px;"><span><i class="fa fa-check"> </i></span><span>Repairs done with shipment to Lenovo Depot (<b>shipping costs covered by Lenovo</b>)</span></p></div></div><div class="partContainer part-container-pos"><ul><li class="starting-at-option" group-minprice="0"><label style="height: 0px;"><span style="text-align:left">One Year</span><span style="text-align:right"><i class="fa fa-pencil"></i></span></label></li></ul><ul><li class="stackableWarranty-optionList-option option_1471572303 selected warranty-option" optionid="option_1471572303" value="" depends="{"Accidental Damage Protection":["5PS0K76367"],"Keep your Drive":["5PS0W48376"]}" group-id="Depot Warranty" option-index="optionIndex_0" isbasewarranty="true"><input type="hidden" id="option_1471572303" name="options_11" value=""><label><div class="btnText" style="height: 55px;"><span>Base Warranty</span><span class="warrantyYear">One Year</span><i class="fa fa-check"></i></div></label></li><li class="stackableWarranty-optionList-option option_1754247872 warranty-option" optionid="option_1754247872" value="5WS0K76348" depends="{"Accidental Damage Protection":["5PS0K76361"],"Keep your Drive":["5PS0W48380"]}" group-id="Depot Warranty" option-index="optionIndex_1" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Two Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 79-0">+ $79.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_234087166 warranty-option" optionid="option_234087166" value="5WS0K76347" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0K76365"],"Keep your Drive":["5PS0W48373"]}" group-id="Depot Warranty" option-index="optionIndex_2" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Three Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 105-0">+ $105.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_1407905887 warranty-option" optionid="option_1407905887" value="5WS0M73802" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0W36590"],"Keep your Drive":["5PS0W36585"]}" group-id="Depot Warranty" option-index="optionIndex_3" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Four year</span></div><div class="option_price_items"><span class="stackablePriceLabel 129-0">+ $129.00</span></div></div></label></li></ul></div></div></div><div class="stackableSection" groupcode="Onsite Warranty NBD" priority="3"><div class="sectionContent sectionContentDisplay"><div class="merchContainer hasImg upgrande-warranty-merchcontainer" style="height: 517px;"><div class="merchExpandArrow"><i class="fa fa-angle-down fa-2x merch-expand-arrow" isexpand="true"></i></div><div class="merchImg"><img src="/medias/onsite.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDIyNDl8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGZiL2gxNi8xMDU0OTkxNDA0MjM5OC5wbmd8MGM0MjZlZTA4MDY0NWZlZGE5OTY4NDA3OGJiYjVhZjFlMTBhZDM0MmU4Y2MwZDMzMzA1NzQ1MzAxZGJhYWZmYg" style="height: 55px;"></div><div class="stackableHeader stackabel-header-pos upgrade-warranty-stackable-header" style="height: 64px;"><span class="merchImg-for-mobile notDisplay"><img src="/medias/onsite.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDIyNDl8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGZiL2gxNi8xMDU0OTkxNDA0MjM5OC5wbmd8MGM0MjZlZTA4MDY0NWZlZGE5OTY4NDA3OGJiYjVhZjFlMTBhZDM0MmU4Y2MwZDMzMzA1NzQ1MzAxZGJhYWZmYg"></span><span class="good-tag-for-mobile notDisplay">BETTER</span><span class="pos_group_title">Onsite Support</span><span class="pos_group_info_icon" attr-code="Onsite Warranty NBD"><img src="/_ui/desktop/common/css/images/pos_group_icon.png"></span><span class="startAt-for-mobile notDisplay">Starting at +$50.00</span><span class="expand-icon-for-mobile notDisplay fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-up fa-stack-1x"></i></span></div><div class="good-better-best-tag hastag" style="height: 44px;"><span class="good-better-best-text">BETTER</span></div><div class="merchText select-warranty-merchtext"><p style="height: 40px;" class="InsidecontentText" row-index="0"><span><i class="fa fa-check"> </i></span><span>Parts & Labor</span></p> <p class="InsidecontentText" row-index="1" style="height: 57px;"><span><i class="fa fa-check"> </i></span><span>Basic Phone Support for hardware issues</span></p> <p class="InsidecontentText" row-index="2" style="height: 77px;"><span><i class="fa fa-check"> </i></span><span><b>Faster</b> - Repairs done <b>at your location</b></span></p></div></div><div class="partContainer part-container-pos"><ul><li class="starting-at-option" group-minprice="50.00"><label style="height: 0px;"><span>Starting at +$50.00</span></label></li></ul><ul><li class="stackableWarranty-optionList-option option_1474912093 warranty-option" optionid="option_1474912093" value="5WS0K76362" depends="{"Accidental Damage Protection":["5PS0K76367"],"Keep your Drive":["5PS0W48376"]}" group-id="Onsite Warranty NBD" option-index="optionIndex_0" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">One Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 50-0">+ $50.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_327536232 warranty-option" optionid="option_327536232" value="5WS0K76349" depends="{"Accidental Damage Protection":["5PS0K76361"],"Keep your Drive":["5PS0W48380"]}" group-id="Onsite Warranty NBD" option-index="optionIndex_1" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Two Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 89-0">+ $89.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_447005205 warranty-option" optionid="option_447005205" value="5WS0K76356" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0K76365"],"Keep your Drive":["5PS0W48373"]}" group-id="Onsite Warranty NBD" option-index="optionIndex_2" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Three Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 129-0">+ $129.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_1952971851 warranty-option" optionid="option_1952971851" value="5WS0W36587" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0W36590"],"Keep your Drive":["5PS0W36585"]}" group-id="Onsite Warranty NBD" option-index="optionIndex_3" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Four year</span></div><div class="option_price_items"><span class="stackablePriceLabel 169-0">+ $169.00</span></div></div></label></li></ul></div></div></div><div class="stackableSection" groupcode="Premium Care" priority="4"><div class="sectionContent sectionContentDisplay"><div class="pd-logodiv"><div class="pd-merch-tagLabel-ribbon" style="background: green;">RECOMMENDED<i class="pd-merch-arrow" style="border-bottom-color: rgb(0, 128, 0); border-top-color: rgb(0, 128, 0);"></i></div></div><div class="merchContainer hasImg upgrande-warranty-merchcontainer" style="height: 517px;"><div class="merchExpandArrow"><i class="fa fa-angle-down fa-2x merch-expand-arrow" isexpand="true"></i></div><div class="merchImg"><img src="/medias/premium-care.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDI0NjN8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGVmL2g2Yy8xMDU1MzYxOTQxNTA3MC5wbmd8OTk2ODlkYzE0YjdlNDQwODcwMWM2OTg4N2E3OTM5NGE4YjAwODBjYWM5YmZlOWJhMzEzYzkxMzUzYTE5NjZkOQ" style="height: 55px;"></div><div class="stackableHeader stackabel-header-pos upgrade-warranty-stackable-header" style="height: 64px;"><span class="merchImg-for-mobile notDisplay"><img src="/medias/premium-care.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDI0NjN8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGVmL2g2Yy8xMDU1MzYxOTQxNTA3MC5wbmd8OTk2ODlkYzE0YjdlNDQwODcwMWM2OTg4N2E3OTM5NGE4YjAwODBjYWM5YmZlOWJhMzEzYzkxMzUzYTE5NjZkOQ"></span><span class="good-tag-for-mobile notDisplay">BEST</span><span class="pos_group_title">Premium Care Onsite Support</span><span class="pos_group_info_icon" attr-code="Premium Care"><img src="/_ui/desktop/common/css/images/pos_group_icon.png"></span><span class="startAt-for-mobile notDisplay">Starting at +$59.00</span><span class="expand-icon-for-mobile notDisplay fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-up fa-stack-1x"></i></span></div><div class="good-better-best-tag hastag" style="height: 44px;"><span class="good-better-best-text">BEST</span></div><div class="merchText select-warranty-merchtext"><p class="InsidecontentText" row-index="0" style="height: 40px;"><span><i class="fa fa-check"> </i></span><span>Parts & Labor</span></p> <p class="InsidecontentText" row-index="1" style="height: 57px;"><span><i class="fa fa-check"> </i></span><span><b>Advanced</b> Phone Support with <b>Dedicated Lenovo Experts</b></span></p> <p class="InsidecontentText" row-index="2" style="height: 77px;"><span><i class="fa fa-check"> </i></span><span><b>Fastest</b> - Repairs done at your location, <b>Next Business Day</b></span></p> <p class="InsidecontentText" row-index="3" style="height: 56px;"><span><i class="fa fa-check"> </i></span><span><b>Comprehensive</b> Hardware & <b>Software Support</b></span></p> <p class="InsidecontentText" row-index="4" style="height: 56px;"><span><i class="fa fa-check"> </i></span><span><b>Getting Started & "How To" Assistance</b></span></p> <p class="InsidecontentText" row-index="5" style="height: 56px;"><span><i class="fa fa-check"> </i></span><span><b>Comprehensive Yearly Health Check</b></span></p></div></div><div class="partContainer part-container-pos"><ul><li class="starting-at-option" group-minprice="59.00"><label style="height: 0px;"><span>Starting at +$59.00</span></label></li></ul><ul><li class="stackableWarranty-optionList-option option_1225403106 warranty-option" optionid="option_1225403106" value="5WS0T73712" depends="{"Accidental Damage Protection":["5PS0K76367"],"Keep your Drive":["5PS0W48376"]}" group-id="Premium Care" option-index="optionIndex_0" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">One Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 59-0">+ $59.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_758229783 warranty-option" optionid="option_758229783" value="5WS0T73710" depends="{"Accidental Damage Protection":["5PS0K76361"],"Keep your Drive":["5PS0W48380"]}" group-id="Premium Care" option-index="optionIndex_1" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Two Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 105-0">+ $105.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_830081606 warranty-option" optionid="option_830081606" value="5WS0T73708" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0K76365"],"Keep your Drive":["5PS0W48373"]}" group-id="Premium Care" option-index="optionIndex_2" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Three Year</span></div><div class="option_price_items"><span class="strike_through">+ $159.00</span><span class="stackablePriceLabel">+ $127.00</span><span class="saving_price">Savings Of $32.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_359776740 warranty-option" optionid="option_359776740" value="5WS0W36573" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0W36590"],"Keep your Drive":["5PS0W36585"]}" group-id="Premium Care" option-index="optionIndex_3" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Four year</span></div><div class="option_price_items"><span class="strike_through">+ $209.00</span><span class="stackablePriceLabel">+ $167.00</span><span class="saving_price">Savings Of $42.00</span></div></div></label></li></ul></div></div></div><div class="choose-option-container"><a class="choose-option"><span class="fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-down fa-stack-1x"></i></span></a><a class="hide-option"><span class="fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-up fa-stack-1x"></i></span></a></div></section>
'''
new_soup = Soup(b, 'html.parser')
#print(new_soup.prettify())
d = new_soup.find_all('li')
for b in range(len(d)):
e = d[b]
print(e)
现在,当我运行代码时,它给了我所有的15个列表。
d = new_soup.find_all('li')
e = d[2]
c = e['optionid']
print(c.strip('option_'))
我得到的输出是'1754247872'
我想要的是一些逻辑e = d[1:15],所以我可以得到所有的选项id一个一个如果存在。
我甚至试过Beautifulsoup method
new_soup = Soup(b, 'html.parser')
d = new_soup.find_all('li')
e = d.find('optionid')
print(e)
得到的错误为AttributeError: ResultSet对象没有属性'find'。您可能将元素列表视为单个元素。当您打算调用find()时,是否调用了find_all() ?">
所有我想要的是获取选项id代码从列表标签,但不知道我应该如何与BeautifulSoup,或硒。
也为第一个例子,我试图获取选项id,因为每个列表标签有相同的类名,只是选项标签是不同的。所以我将首先得到所有的选项id,然后通过使用f字符串我可以附加它使用for循环1 by 1获取数据。
我不知道我做的是对还是错,但让我们往好处想吧。
发生了什么?
如前所述,d
是list
,而find()
没有属性。
如何修复?
选择所有具有optionid
属性的<li>
并对其进行迭代以打印:
for li in new_soup.select('li[optionid]'):
print(li['optionid'].split('_')[-1])
或生成一个值为
的列表:[li['optionid'].split('_')[-1] for li in new_soup.select('li[optionid]')]
例子from bs4 import BeautifulSoup as Soup
import json
b = '''
<section class="warr-section is-expand" id="upgradeWarranty"><span class="show-differences differences-button notDisplay"></span><span class="hide-differences differences-button notDisplay"></span><div class="stackableSection stackable-selected" groupcode="Depot Warranty" priority="2"><div class="sectionContent sectionContentDisplay"><div class="merchContainer hasImg upgrande-warranty-merchcontainer" style="height: 517px;"><div class="merchExpandArrow"><i class="fa fa-angle-down fa-2x merch-expand-arrow" isexpand="true"></i></div><div class="merchImg"><img src="/medias/depot.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDIzMTB8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGYyL2gzMi8xMDU0OTkxMzcxNDcxOC5wbmd8ZTIwNGYwMjQxZTY1YjIzNjRmNGRlZDFmZTgzMmY2YTgwYThhZGNjZDg2MGFhMWFjYzkzZjU3MjAyNzMzZGZmNw" style="height: 55px;"></div><div class="stackableHeader stackabel-header-pos upgrade-warranty-stackable-header" style="height: 64px;"><span class="merchImg-for-mobile notDisplay"><img src="/medias/depot.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDIzMTB8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGYyL2gzMi8xMDU0OTkxMzcxNDcxOC5wbmd8ZTIwNGYwMjQxZTY1YjIzNjRmNGRlZDFmZTgzMmY2YTgwYThhZGNjZDg2MGFhMWFjYzkzZjU3MjAyNzMzZGZmNw"></span><span class="good-tag-for-mobile notDisplay">GOOD</span><span class="pos_group_title">Depot Support</span><span class="pos_group_info_icon" attr-code="Depot Warranty"><img src="/_ui/desktop/common/css/images/pos_group_icon.png"></span><span class="startAt-for-mobile notDisplay">One Year</span><span class="expand-icon-for-mobile notDisplay fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-up fa-stack-1x"></i></span></div><div class="good-better-best-tag hastag selected-group" style="height: 44px;"><span class="good-better-best-text">GOOD</span></div><div class="merchText select-warranty-merchtext"><p style="height: 40px;" class="InsidecontentText" row-index="0"><span><i class="fa fa-check"> </i></span><span>Parts & Labor</span></p> <p class="InsidecontentText" row-index="1" style="height: 57px;"><span><i class="fa fa-check"> </i></span><span>Basic Phone Support for hardware issues</span></p> <p class="InsidecontentText" row-index="2" style="height: 77px;"><span><i class="fa fa-check"> </i></span><span>Repairs done with shipment to Lenovo Depot (<b>shipping costs covered by Lenovo</b>)</span></p></div></div><div class="partContainer part-container-pos"><ul><li class="starting-at-option" group-minprice="0"><label style="height: 0px;"><span style="text-align:left">One Year</span><span style="text-align:right"><i class="fa fa-pencil"></i></span></label></li></ul><ul><li class="stackableWarranty-optionList-option option_1471572303 selected warranty-option" optionid="option_1471572303" value="" depends="{"Accidental Damage Protection":["5PS0K76367"],"Keep your Drive":["5PS0W48376"]}" group-id="Depot Warranty" option-index="optionIndex_0" isbasewarranty="true"><input type="hidden" id="option_1471572303" name="options_11" value=""><label><div class="btnText" style="height: 55px;"><span>Base Warranty</span><span class="warrantyYear">One Year</span><i class="fa fa-check"></i></div></label></li><li class="stackableWarranty-optionList-option option_1754247872 warranty-option" optionid="option_1754247872" value="5WS0K76348" depends="{"Accidental Damage Protection":["5PS0K76361"],"Keep your Drive":["5PS0W48380"]}" group-id="Depot Warranty" option-index="optionIndex_1" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Two Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 79-0">+ $79.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_234087166 warranty-option" optionid="option_234087166" value="5WS0K76347" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0K76365"],"Keep your Drive":["5PS0W48373"]}" group-id="Depot Warranty" option-index="optionIndex_2" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Three Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 105-0">+ $105.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_1407905887 warranty-option" optionid="option_1407905887" value="5WS0M73802" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0W36590"],"Keep your Drive":["5PS0W36585"]}" group-id="Depot Warranty" option-index="optionIndex_3" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Four year</span></div><div class="option_price_items"><span class="stackablePriceLabel 129-0">+ $129.00</span></div></div></label></li></ul></div></div></div><div class="stackableSection" groupcode="Onsite Warranty NBD" priority="3"><div class="sectionContent sectionContentDisplay"><div class="merchContainer hasImg upgrande-warranty-merchcontainer" style="height: 517px;"><div class="merchExpandArrow"><i class="fa fa-angle-down fa-2x merch-expand-arrow" isexpand="true"></i></div><div class="merchImg"><img src="/medias/onsite.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDIyNDl8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGZiL2gxNi8xMDU0OTkxNDA0MjM5OC5wbmd8MGM0MjZlZTA4MDY0NWZlZGE5OTY4NDA3OGJiYjVhZjFlMTBhZDM0MmU4Y2MwZDMzMzA1NzQ1MzAxZGJhYWZmYg" style="height: 55px;"></div><div class="stackableHeader stackabel-header-pos upgrade-warranty-stackable-header" style="height: 64px;"><span class="merchImg-for-mobile notDisplay"><img src="/medias/onsite.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDIyNDl8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGZiL2gxNi8xMDU0OTkxNDA0MjM5OC5wbmd8MGM0MjZlZTA4MDY0NWZlZGE5OTY4NDA3OGJiYjVhZjFlMTBhZDM0MmU4Y2MwZDMzMzA1NzQ1MzAxZGJhYWZmYg"></span><span class="good-tag-for-mobile notDisplay">BETTER</span><span class="pos_group_title">Onsite Support</span><span class="pos_group_info_icon" attr-code="Onsite Warranty NBD"><img src="/_ui/desktop/common/css/images/pos_group_icon.png"></span><span class="startAt-for-mobile notDisplay">Starting at +$50.00</span><span class="expand-icon-for-mobile notDisplay fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-up fa-stack-1x"></i></span></div><div class="good-better-best-tag hastag" style="height: 44px;"><span class="good-better-best-text">BETTER</span></div><div class="merchText select-warranty-merchtext"><p style="height: 40px;" class="InsidecontentText" row-index="0"><span><i class="fa fa-check"> </i></span><span>Parts & Labor</span></p> <p class="InsidecontentText" row-index="1" style="height: 57px;"><span><i class="fa fa-check"> </i></span><span>Basic Phone Support for hardware issues</span></p> <p class="InsidecontentText" row-index="2" style="height: 77px;"><span><i class="fa fa-check"> </i></span><span><b>Faster</b> - Repairs done <b>at your location</b></span></p></div></div><div class="partContainer part-container-pos"><ul><li class="starting-at-option" group-minprice="50.00"><label style="height: 0px;"><span>Starting at +$50.00</span></label></li></ul><ul><li class="stackableWarranty-optionList-option option_1474912093 warranty-option" optionid="option_1474912093" value="5WS0K76362" depends="{"Accidental Damage Protection":["5PS0K76367"],"Keep your Drive":["5PS0W48376"]}" group-id="Onsite Warranty NBD" option-index="optionIndex_0" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">One Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 50-0">+ $50.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_327536232 warranty-option" optionid="option_327536232" value="5WS0K76349" depends="{"Accidental Damage Protection":["5PS0K76361"],"Keep your Drive":["5PS0W48380"]}" group-id="Onsite Warranty NBD" option-index="optionIndex_1" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Two Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 89-0">+ $89.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_447005205 warranty-option" optionid="option_447005205" value="5WS0K76356" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0K76365"],"Keep your Drive":["5PS0W48373"]}" group-id="Onsite Warranty NBD" option-index="optionIndex_2" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Three Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 129-0">+ $129.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_1952971851 warranty-option" optionid="option_1952971851" value="5WS0W36587" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0W36590"],"Keep your Drive":["5PS0W36585"]}" group-id="Onsite Warranty NBD" option-index="optionIndex_3" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Four year</span></div><div class="option_price_items"><span class="stackablePriceLabel 169-0">+ $169.00</span></div></div></label></li></ul></div></div></div><div class="stackableSection" groupcode="Premium Care" priority="4"><div class="sectionContent sectionContentDisplay"><div class="pd-logodiv"><div class="pd-merch-tagLabel-ribbon" style="background: green;">RECOMMENDED<i class="pd-merch-arrow" style="border-bottom-color: rgb(0, 128, 0); border-top-color: rgb(0, 128, 0);"></i></div></div><div class="merchContainer hasImg upgrande-warranty-merchcontainer" style="height: 517px;"><div class="merchExpandArrow"><i class="fa fa-angle-down fa-2x merch-expand-arrow" isexpand="true"></i></div><div class="merchImg"><img src="/medias/premium-care.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDI0NjN8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGVmL2g2Yy8xMDU1MzYxOTQxNTA3MC5wbmd8OTk2ODlkYzE0YjdlNDQwODcwMWM2OTg4N2E3OTM5NGE4YjAwODBjYWM5YmZlOWJhMzEzYzkxMzUzYTE5NjZkOQ" style="height: 55px;"></div><div class="stackableHeader stackabel-header-pos upgrade-warranty-stackable-header" style="height: 64px;"><span class="merchImg-for-mobile notDisplay"><img src="/medias/premium-care.png?context=bWFzdGVyfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvfDI0NjN8aW1hZ2UvcG5nfC93dy9pY29ucy9pbnRlcnN0aXRpYWwvc2VydmljZXMvaGVmL2g2Yy8xMDU1MzYxOTQxNTA3MC5wbmd8OTk2ODlkYzE0YjdlNDQwODcwMWM2OTg4N2E3OTM5NGE4YjAwODBjYWM5YmZlOWJhMzEzYzkxMzUzYTE5NjZkOQ"></span><span class="good-tag-for-mobile notDisplay">BEST</span><span class="pos_group_title">Premium Care Onsite Support</span><span class="pos_group_info_icon" attr-code="Premium Care"><img src="/_ui/desktop/common/css/images/pos_group_icon.png"></span><span class="startAt-for-mobile notDisplay">Starting at +$59.00</span><span class="expand-icon-for-mobile notDisplay fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-up fa-stack-1x"></i></span></div><div class="good-better-best-tag hastag" style="height: 44px;"><span class="good-better-best-text">BEST</span></div><div class="merchText select-warranty-merchtext"><p class="InsidecontentText" row-index="0" style="height: 40px;"><span><i class="fa fa-check"> </i></span><span>Parts & Labor</span></p> <p class="InsidecontentText" row-index="1" style="height: 57px;"><span><i class="fa fa-check"> </i></span><span><b>Advanced</b> Phone Support with <b>Dedicated Lenovo Experts</b></span></p> <p class="InsidecontentText" row-index="2" style="height: 77px;"><span><i class="fa fa-check"> </i></span><span><b>Fastest</b> - Repairs done at your location, <b>Next Business Day</b></span></p> <p class="InsidecontentText" row-index="3" style="height: 56px;"><span><i class="fa fa-check"> </i></span><span><b>Comprehensive</b> Hardware & <b>Software Support</b></span></p> <p class="InsidecontentText" row-index="4" style="height: 56px;"><span><i class="fa fa-check"> </i></span><span><b>Getting Started & "How To" Assistance</b></span></p> <p class="InsidecontentText" row-index="5" style="height: 56px;"><span><i class="fa fa-check"> </i></span><span><b>Comprehensive Yearly Health Check</b></span></p></div></div><div class="partContainer part-container-pos"><ul><li class="starting-at-option" group-minprice="59.00"><label style="height: 0px;"><span>Starting at +$59.00</span></label></li></ul><ul><li class="stackableWarranty-optionList-option option_1225403106 warranty-option" optionid="option_1225403106" value="5WS0T73712" depends="{"Accidental Damage Protection":["5PS0K76367"],"Keep your Drive":["5PS0W48376"]}" group-id="Premium Care" option-index="optionIndex_0" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">One Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 59-0">+ $59.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_758229783 warranty-option" optionid="option_758229783" value="5WS0T73710" depends="{"Accidental Damage Protection":["5PS0K76361"],"Keep your Drive":["5PS0W48380"]}" group-id="Premium Care" option-index="optionIndex_1" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Two Year</span></div><div class="option_price_items"><span class="stackablePriceLabel 105-0">+ $105.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_830081606 warranty-option" optionid="option_830081606" value="5WS0T73708" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0K76365"],"Keep your Drive":["5PS0W48373"]}" group-id="Premium Care" option-index="optionIndex_2" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Three Year</span></div><div class="option_price_items"><span class="strike_through">+ $159.00</span><span class="stackablePriceLabel">+ $127.00</span><span class="saving_price">Savings Of $32.00</span></div></div></label></li><li class="stackableWarranty-optionList-option option_359776740 warranty-option" optionid="option_359776740" value="5WS0W36573" depends="{"Sealed Battery Replacement":["5WS0V98154"],"Accidental Damage Protection":["5PS0W36590"],"Keep your Drive":["5PS0W36585"]}" group-id="Premium Care" option-index="optionIndex_3" isbasewarranty="false"><label><div class="btnText" style="height: 55px;"><div class="option_price_label"><span></span><span class="warrantyYear">Four year</span></div><div class="option_price_items"><span class="strike_through">+ $209.00</span><span class="stackablePriceLabel">+ $167.00</span><span class="saving_price">Savings Of $42.00</span></div></div></label></li></ul></div></div></div><div class="choose-option-container"><a class="choose-option"><span class="fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-down fa-stack-1x"></i></span></a><a class="hide-option"><span class="fa-stack fa-lg"><i class="fa fa-circle-thin fa-stack-2x"></i><i class="fa fa-angle-up fa-stack-1x"></i></span></a></div></section>
'''
new_soup = Soup(b, 'html.parser')
for li in new_soup.select('li[optionid]'):
print(li['optionid'].split('_')[-1])
输出1471572303
1754247872
234087166
1407905887
1474912093
327536232
447005205
1952971851
1225403106
758229783
830081606
359776740
try:
...
for li in new_soup.find_all("li"):
for attr in li.attrs.get('class', []):
if attr.startswith('option_'):
print(attr.split("_")[-1])
1471572303
1754247872
234087166
1407905887
1474912093
327536232
447005205
1952971851
1225403106
758229783
830081606
359776740