2015-11-08 47 views
0

我想從一個網頁內容選擇一些細節如何在Xpath中選擇文本?

<div id="realty_widget_property_description" class="property_block realty_widget_property_description widget widget-2 alt"> 
<p class="title "><strong>Apartment for Rent </strong></p> 
<div class="property-description "> 
     <p class="property_description"> 
     </p><p>– Property ID: P-000324<br> 
– Location: Phsar Chas<br> 
– Price: $350/month<br> 
– Bed/Bath: 2 beds/2 Baths<br> 
– Size: 4m x 18m<br> 
– Story: 1st<br> 
– Fully Furnished<br> 
– Description: This apartment is in residential area and good location for living with comfortable and fresh air. There are 2 bedrooms, 2 bathrooms, a living room, a kitchen and the balcony. Close to everywhere in town such as school, market, clinic, Bank, coffee shop and just ride a few minutes to Phsar Chas Market.<br> 

我如何選擇物業編號,地點,價格,尺寸,故事和說明,如果他們都留在同一個班?

+0

您使用的是哪個版本的XPath,您想要哪種數據?在您顯示的最後一個'p'元素中有文本節點,您可以用例如'// div [@id ='realty_widget_property_description']/div [@class ='property-description']/p [2]/text()',這會給你例如' - 房產編號:P-000324',' - 位置:Phsar Chas'等。 –

+0

我想一個一個選擇它而不是全選 – july77

+0

然後添加一個位置謂詞,例如'/ div [@id ='realty_widget_property_description']/div [@class ='property-description']/p [2]/text()[1]'給你一個字符串值爲' - Property ID :P-000324'。 –

回答

1

使用例如substring-after(//div[@id = 'realty_widget_property_description']/div[@class = 'property-description ']/p[2]/text()[1], ': ')提取字符串值。但請注意,XPath表達式返回一個字符串值而不是一個節點,因此您需要確保使用支持該值的XPath API。

+0

Mr.Martin Honnen你能幫我嗎?我的代碼不工作echo $ xpath-> evaluate('// div [contains(@id,「realty_widget_property_description」)]/div [contains(@class,「property-description」)]/p [2]/text )[1]') - >項(0) - >的nodeValue;我只想要P-00324 – july77

+0

那麼哪種編程語言就是那種,你使用的是哪種XPath API?如果這是與PHP的XPath,那麼我認爲''xpath-> evaluate(「substring-after(// div [@id ='realty_widget_property_description']/div [@class ='property-description']/p [2 ]/text()[1],':')「)'會返回一個想要的結果的字符串。 –

+0

!非常感謝你。有一個愉快的一天:):):)<3 – july77