如何打印xpath解析器

时间:2017-06-19 08:40:22

标签: xml python-2.7 xpath

我在Python 2.7 / Flask 0.12中解析lxml时遇到了一些麻烦。

我想打印已解析的xpath数据,但无法找到方法。 这是我的代码。

from lxml import html
from lxml import etree

def parse_html(html_src):
    target_xpath = //div[@class="primary-content"]//div[@class="mini-cart-product clearfix"]'

    detail_html = html.fromstring(html_src)
    page_tree = etree.ElementTree(detail_html)

    target_value_list = page_tree.xpath(target_xpath)
    #want to print 'target_value_list'

    return target_value_list

这里是html_src

<div class="mini-cart-product clearfix">
    <div class="mini-cart-image">
        <a href="/carters-baby-boy-one-pieces/190795419986.html"><img src="https://www.carters.com/dw/image/v2/AAMK_PRD/on/demandware.static/-/Sites-carters_master_catalog/default/dw540ec9a5/hi-res/127G525_Default.jpg?sw=470" alt="Neon Little Brother Jumpsuit" title="Neon Little Brother Jumpsuit"></a>
        <div class="mini-cart-brand">
            <div class="carters"></div>
        </div>
    </div>
    <div class="mini-cart-attributes">
        <div class="product-name">
            <a href="/carters-baby-boy-one-pieces/190795419986.html">Neon Little Brother Jumpsuit</a>
        </div>
        <div class="attribute Size">
            <span class="label">Size:</span>
            <span class="value">
        9M
        </span>
        </div>
        <div class="verticalLine">|</div>
        <div class="attribute Color">
            <span class="label">Color:</span>
            <span class="value">
        Blue
        </span>
        </div>
        <div class="minicartpricedisplay">
            <div class="price">
        <span class="MSRP price-standard">
        <span class="msrp">MSRP*:
        $14.00
        </span>
        </span>
                <span class="price-standard ">$6.00</span>
            </div>
        </div>
    </div>
</div>
<div class="mini-cart-product clearfix">
    <div class="mini-cart-image">
        <a href="/carters-baby-boy-one-pieces/190795039832.html"><img src="https://www.carters.com/dw/image/v2/AAMK_PRD/on/demandware.static/-/Sites-carters_master_catalog/default/dw182a85c8/hi-res/118H023_Default.jpg?sw=470" alt="Piqué Polo Romper" title="Piqué Polo Romper"></a>
        <div class="mini-cart-brand">
            <div class="carters"></div>
        </div>
    </div>
    <div class="mini-cart-attributes">
        <div class="product-name">
            <a href="/carters-baby-boy-one-pieces/190795039832.html">Piqué Polo Romper</a>
        </div>
        <div class="attribute Size">
            <span class="label">Size:</span>
            <span class="value">
        12M
        </span>
        </div>
        <div class="verticalLine">|</div>
        <div class="attribute Color">
            <span class="label">Color:</span>
            <span class="value">
        Blue
        </span>
        </div>
        <div class="minicartpricedisplay">
            <div class="price">
        <span class="MSRP price-standard">
        <span class="msrp">MSRP*:
        $18.00
        </span>
        </span>
                <span class="price-standard desktopvisible">$7.20</span>
            </div>
        </div>
    </div>
</div>

如果我尝试打印&#39; target_value_list&#39;,则会打印一些内存地址列表。 请有人救救我。

我想打印traget_value_list中的内容。想知道它是否包含html_src中的所有项目。

1 个答案:

答案 0 :(得分:1)

如果您想获取文本值列表而不是webelements列表,可以尝试在.insertBefore()中使用/text()语法:例如,使用XPath或{{1而不仅仅是//div/text()或使用//div//text()属性或//div方法,如下所示:

text