仅当前面的项具有标识符时,使用xpath选择文本

时间:2016-07-20 18:23:31

标签: html xpath

我的代码遵循以下格式,如下所示。我正在尝试使用xpath来选择第2段(例如开始的段落:辅助生活是各行各业的一个很好的选择......鉴于div和段落没有简单的标识符,我试过:

//div[preceding-sibling::div[@id='w_29207']]/p[2]

我认为第一部分//div....29207']]会选择前面带有id为29207的div的div。然后/ p [2]会选择第2段。< / p>

<div id="w_29207" class=" city short-description">
<div>
<p>
<span itemscope itemtype="http://schema.org/Product">
<meta itemprop="name" content="Albuquerque, NM Assisted Living Facilities" />Choose from over 38 Assisted Living communities in Albuquerque, NM and 
<span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">see
<meta itemprop="reviewCount" content="360 reviews "/>360 reviews with a
<meta itemprop="ratingValue" content="4.0 stars out of 5" />4.0 average rating.
<meta itemprop="description" content="Albuquerque attracts seniors looking for an active senior living experience. The city enjoys four distinct seasons with stunning winters and sunny summers. Seniors in Albuquerque find various cultural outlets and outdoor recreation including an active theater community, festivals, hiking and climbing. " /> We've helped 9,287 families in Albuquerque, NM where Assisted Living ranges from $</span><span itemprop='offerDetails' itemscope itemtype='http://schema.org/AggregateOffer'><span content='1,550' itemprop='lowPrice'>1,550</span> to $<span content='6,100' itemprop='highPrice'>6,100</span>.
<meta content='USD' itemprop='priceCurrency'>

</span></span></p>

<p>Assisted living is a great option for all walks of life. Whether you or a loved has experienced a life changing health diagnosis or you want to shed the burdens of home maintenance from your life, assisted living in Albuquerque could be the perfect match for you. Many seniors in Albuquerque have chosen to transition to assisted living so they can enjoy a sense of community with like-minded neighbors. Another perk of assisted living is for seniors that may need assistance with daily tasks such as, meal preparation, medication management, or help getting to appointments.
</p>

3 个答案:

答案 0 :(得分:0)

如果p元素是兄弟,那么

p [2]将起作用。

部分选项:

&#13;
&#13;
//*[contains(@class, 'city short-description')]/div/p[2]

//*[@class='city short-description']/div/p[2]

//div[contains(@class, 'city')]/div/p[2]

//div[contains(@id, 'w_')]/div/p[2]
&#13;
&#13;
&#13;

如果您需要返回文本而不是元素,请在p [2]

之后添加/ text()

答案 1 :(得分:0)

问题似乎是第二个<div>不是<div id="w_29207">的兄弟,它是它的孩子(除非你发布的HTML是错误的)。这就是//div[preceding-sibling::div[@id='w_29207']]未选择第二个<div>

的原因

相反,您可以使用

//div[@id='w_29207']/div[1]/p[2]

或@lauda提到的任何选项,具体取决于您对id属性的准确程度。

答案 2 :(得分:0)

您也可以尝试关注xpath: -

//p[not(child::*)]

//p[ancestor::div[@id='w_29207']][2]

//div[@id='w_29207']/descendant::p[2]

//p[ancestor::div[@id='w_29207'] and not(child::*)]   

希望它有所帮助.. :)