Not able to extract data using scrapy with class names containing spaces and hyphens

Viewed 8094

I am new to scrapy and I have to extract text from a tag with multiple class names, where the class names contain spaces and hyphens.

Example:

<div class="info">
    <span class="price sale">text1</span>
    <span class="title ng-binding">some text</span>
</div>

When i use the code:

response.xpath("//span[contains(@class,'price sale')]/text()").extract()

I am able to get text1 but when I use:

response.xpath("//span[contains(@class,'title ng-binding')]/text()").extract()

I get an empty list. Why is this happening and how to handle this?

2 Answers

You can replace the spaces with "." in your code when using response.css(). In your case you can try:

response.css("span.title.ng-binding::text").extract()

This code should return the text you are looking for.

Related