[英]How to return single string value of XPath expression?
This is my HTML: 这是我的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. 我想在一个 XPath表达式中获取整个文本。
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? 但它只返回第一个
br
之前的文本(我知道为什么,那是因为我使用.extract()[0]
,如果我使用.extract()[1]和[2] ....我会得到的我想要什么,但我必须使用.extract [0],因为它是一个平台就是这样。是否有任何XPath返回整个文本,但在一个字符串而不是多个字符串?
string(/)
will return the string value of the whole document. string(/)
将返回整个文档的字符串值。
Update : To return the four separate strings returned by this XPath, 更新 :要返回此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()
类似地包装上面的XPath:
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. 更新2 :但是
br
和text()
动作不是必需的。 You can simply get the string value of the p
: 你可以简单地得到
p
的字符串值:
string(.//h3[@class='description-area']/following-sibling::p)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.