Akhil Reddy
Akhil Reddy

Reputation: 391

Scrapy excluding blank spaces

I have tried scrape a link(https://www.century21.com/real-estate/rock-spring-ga/LCGAROCKSPRING/). I wanted to extract "beds" data from that. since there are some empty details for some houses in that, only few "beds" details are extracted. But i want to have the details for all. If not there, then it should show "NaN" or something. Another problem is that i have inspected this tag:

<div class="property-beds">
            <strong>2</strong> beds
         </div>

i used the xpath command to extract "2 beds". Only "beds" is displayed nOT "2 beds". So i used "|" to unify the 2 elements.

response.xpath('//div[@class="property-beds"]/strong/text() | //div[@class="property-beds"]/text()'] 

this got me the correct output but the problem is it is showing result in 2 seperate lines(2 in one line and beds in another line).how to display in single line?

Upvotes: 1

Views: 368

Answers (2)

user8931234
user8931234

Reputation:

Use string() method.root.xpath('string(//div[@class="property-beds"])')

Upvotes: 0

Blender
Blender

Reputation: 298246

You can use string() on the parent element to extract the text of the element and all child nodes:

In [10]: root.xpath('string(//div[@class="property-beds"])')
Out[10]: '2 beds'

If you have more than one element, you'll have to iterate through the elements matched by //div[@class="property-beds"] and then do elem.xpath('string()').

Upvotes: 2

Related Questions