sujit
sujit

Reputation: 113

xpath: How do I extract text within the "strong" tag?

I'm using scrapy and need to extract "Gray / Gray" using xpath selectors. Here's the html snippet:

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

I'm stuck trying to extract "Gray / Gray" within the "strong" tag. Any help is appreciated.

Upvotes: 2

Views: 2564

Answers (2)

gangabass
gangabass

Reputation: 10666

This XPath will work in Scrapy and also in Google/Firefox Developer's Console:

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

You can use this code in your spider:

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

Upvotes: 3

zx485
zx485

Reputation: 29022

You can use this XPath expression with your sample XML/HTML:

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

A full XPath given the full file mentioned below with respect to a namespace "http://www.w3.org/1999/xhtml" can be

/html/body/div/div/div[@class='content-bg']/div/div/div[@class='Vehicle-Overview']/table[@class='Table-Specs']/tr[2]/td[1]/strong[1]

Upvotes: 1

Related Questions