首页 > 解决方案 > xpath:如何提取“强”标签中的文本?

问题描述

我正在使用scrapy,需要使用xpath 选择器提取“灰色/灰色”。这是html片段:

<div class="Vehicle-Overview">
    <div class="Txt-YMM">
        2006 GMC Sierra 1500
    </div>
    <div class="Txt-Price">
        Price :                                     $8,499
    </div>

    <table width="100%" border="0" cellpadding="0" cellspacing="0" 
    class="Table-Specs">
        <tr>
            <td>
                <strong>2006 GMC Sierra 1500 Crew Cab 143.5 WB 4WD 
                SLE</strong>
                <strong class="text-right t-none"></strong>
            </td>
        </tr>
        <tr>
            <td>
                <strong>Gray / Gray</strong><br />
                <strong>209,123 
                            Miles

                                  / VIN: XXXXXXXXXX

            </td>
       </tr>
</table>

我一直试图在“强”标签中提取“灰色/灰色”。任何帮助表示赞赏。

标签: python-3.xxpathscrapy

解决方案


这个 XPath 可以在 Scrapy 和 Google/Firefox 开发者控制台中工作:

//div[@class='Vehicle-Overview']/table[@class='Table-Specs']//tr[2]/td[1]/strong[1]/text()

您可以在您的蜘蛛中使用此代码:

color = response.xpath("//div[@class='Vehicle-Overview']/table[@class='Table-Specs']//tr[2]/td[1]/strong[1]/text()").extract_first()

推荐阅读