1

I want to display http:///gb/groceries/easter-essentials--%28approx-205kg%29.

In scrapy I used this XPath expression:

response.xpath('//div[@class="productNameAndPromotions"]/h3/a/href').extract()

but it didn't work!

<div class="product ">
    <div class="productInfo">
        <div class="productNameAndPromotions">
            <h3>
                <a href="http:///gb/groceries/easter-essentials--%28approx-205kg%29">
                    <img src="http:co.uk/wcsstore7.20.1.145/ExtendedSitesCatalogAssetStore/image/catalog/productImages/08/020000008_L.jpeg" alt="" />
                </a>
            </h3>
        </div>        
    </div>
</div>
1
  • I completed your XML sample to be well-formed. Commented Mar 24, 2017 at 18:03

1 Answer 1

1

This //div[@class="productNameAndPromotions"]/h3/a/href means you want to get element href which is child of a.

If you want to extract nodes' attribute, e.g. href, you need to use @attribute syntax. Try below:

//div[@class="productNameAndPromotions"]/h3/a/@href
Sign up to request clarification or add additional context in comments.

9 Comments

Can you share page URL?
thank you @anderson ! i found another solution , but i had another question aboutscrapy how can i convert the list of strings into floats i generated with (i tried many solutions but doesn't work): >>prices_unit = response.xpath('//p[@class="pricePerUnit"]/text()').extract() >>for title in titles: item["price_unit"] = prices_unit any help please
If you get list of strings with prices_unit = response.xpath('//p[@class="pricePerUnit"]/text()').extract(‌​) then you can simply get list of floats with prices_unit = [float(unit) for unit in prices_unit]
But you said that prices_unit is list of strings, right? :) show how does it really looks like- what is output of print(prices_unit)?
It displays prices , if we print prices_unit {£2.00], £2.50, £1.00, £3.00 ...} and i made re.findall to print only numbers {2.00, 2.50, 1.00, 3.00}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.