Marco Dinatsoli Marco Dinatsoli - 7 months ago 22
HTML Question

How to return single string value of XPath expression?

This is my HTML:

<?xml version="1.0" encoding="UTF-8"?>

<div class="single-main">
<h3 class="description-area">Description</h3>
<p>bla bla bla
<br/> some text
<br/> some text here ,
<br/> other text here
</p>
</div>


I want to get the whole text but in one XPath expression.

This is my code:

response.xpath(".//h3[@class='description-area']/following-sibling::p
//text()[count(preceding-sibling::br) >= 0]").extract()[0]


but it returns just the text before the first
br
(I know why, and that's because I am using
.extract()[0]
and if i used .extract()[1] and [2] .... I will get what I want, but I must use .extract[0] because it is a platform that does just that. Is there any XPath to return the whole text but in one string rather than in multiple strings?

Answer

string(/) will return the string value of the whole document.


Update: To return the four separate strings returned by this XPath,

.//h3[@class='description-area']/following-sibling::p//text()[count(preceding-sibling::br) >= 0]

as a single string, wrap the above XPath similarly in string():

string(.//h3[@class='description-area']/following-sibling::p//text()[count(preceding-sibling::br) >= 0])

Update 2: But the br and text() maneuvers aren't necessary. You can simply get the string value of the p (as you rightly observed in the comments):

string(.//h3[@class='description-area']/following-sibling::p)