使用bs4获取孙子节点

时间:2018-08-14 22:28:59

标签: python web-scraping beautifulsoup

我有这个html。我正在尝试提取跨度标签内的所有文本。我需要检查三件事。顶级节点将为ul,其属性为class =“ a-unordered-list a-vertical a-spacing-none”>。接下来是li标签。它不会有任何attr(不知道如何检查)。具有attr class =“ a-list-item”的下一个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>

经过几次反复试验后,它无法正常工作。请帮忙。

1 个答案:

答案 0 :(得分:0)

尝试一下:

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())

希望这就是您想要的:)