如何返回XPath表达式的单个字符串值? [英] How to return single string value of XPath expression?

查看:98
本文介绍了如何返回XPath表达式的单个字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML:

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

< div class =single-main>
< h3 class =description-area>说明< / h3>
< p> bla bla bla
< br />一些文字
< br />这里有一些文字,
< br />其他文字在这里
< / p>
< / div>

我想要获取整个文本,但只能在一个 XPath表达式中使用



这是我的代码:

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

但它只返回第一个 br .extract()[0] ,如果我使用.extract()[1]和[2] ....我会得到我想要的,但是必须使用.extract [0]是因为它是一个可以做到这一点的平台。是否有任何XPath可以返回整个文本,但是只有一个字符串而不是多个字符串?

解决方案

string(/)将会返回整个文档的字符串值。




更新:要返回四个单独的字符串通过这个XPath,

  .// h3 [@ class ='description-area'] / following-sibling :: p // text()[count(before-sibling作为单个字符串,将上面的XPath包装成类似于 

code> string():

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






更新2 :但 br 和 text()操作是不必要的。您只需获取 p 的字符串值:

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


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?

解决方案

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:

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

这篇关于如何返回XPath表达式的单个字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆