使用bs4获取孙子节点

2024-06-16 10:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我随身带着这个html。我正在尝试提取span标记中的所有文本。我需要检查三件事。顶部节点将是ul,并且在其属性中将具有class=“a-unordered-list a-vertical a-spacing-none”>;。下一个是李泰格。它将没有任何属性(不知道如何检查这个)。下一个带有attr^{cl 1}的ul span标记$

我试着用这个密码-

for line in soup.find_all('ul',attrs={"class" : "a-unordered-list a-vertical a-spacing-none"}):
    for inner_lines in soup.findChildren('li'):
        for inner_inner_lines in soup.findChildren('span',attrs={"class" : "a-list-item"}):
            print(inner_inner_lines.text.split())

对于此html-

<ul class="a-unordered-list a-vertical a-spacing-none">
    Make sure this fits by entering your model number

    <div id="hsx-rpp-bullet-fits-message" class="aok-hidden">
        <div class="a-box a-alert-inline a-alert-inline-success hsx-rpp-fitment-bullets">
            <div class="a-box-inner a-alert-container"><i class="a-icon a-icon-alert"></i>
                <div class="a-alert-content">
                    This fits your&nbsp;<span class="hsx-rpp-bullet-model-info"></span>.
                </div>
            </div>
        </div>
    </div>

    <li id="replacementPartsFitmentBullet" data-doesntfitmessage="We're not sure this item fits your " data-fitsmessage="This fits your " class="aok-hidden"><span class="a-list-item">
        <span id="replacementPartsFitmentBulletInner"> <a class="a-link-normal hsx-rpp-fitment-focus" href="#">Make sure this fits</a>
                <span>by entering your model number.</span>
        </span>
        </span>
    </li>

    <script type="text/javascript">
        P.when("ReplacementPartsBulletLoader").execute(function(module) {
            module.initializeDPX();
        })
    </script>

    <li><span class="a-list-item"> 
                            Powerful 8th Generation Intel Core i5-8250U 1.6GHz (Turbo up to 3.4GHz) processor

                        </span></li>

    <li><span class="a-list-item"> 
                            15.6" Full HD WideView display with ASUS Splendid software enhancement

                        </span></li>

    <li><span class="a-list-item"> 
                            14.2" wide, 0.8" thin and portable footprint with 0.3" ASUS NanoEdge bezel for a stunning 80% screen-to-body ratio

                        </span></li>

    <li><span class="a-list-item"> 
                            8GB DDR4 RAM and 128GB SSD + 1TB HDD storage combo; Ergonomic chiclet keyboard with fingerprint sensor

                        </span></li>

    <li><span class="a-list-item"> 
                            Comprehensive connections including USB 3.1 Type-C (Gen1), USB 3.0, USB 2.0, and HDMI; Lightning-fast 802.11ac Wi-Fi keeps you connected through any congestion or interference

                        </span></li>

</ul>

经过几次反复试验,它不起作用了。请帮忙。你知道吗


Tags: divforyouralertliitemullist
1条回答
网友
1楼 · 发布于 2024-06-16 10:30:08

试试这个:

ul = soup.find('ul', {'class':'a-unordered-list a-vertical a-spacing-none'})
li = ul.findChildren('li', id=False, recursive=False)
for child in li:
    span = child.find_all('span', {'class':'a-list-item'})
    for node3 in span:
        print(node3.get_text().strip())

希望这就是你要找的:)

相关问题 更多 >