get a data with HTMLagilitypack from a span

asp.net c# html html-agility-pack xpath

Question

I want to catch some data from a website using HtmlAgilityPack. The data is stored in an object with the property class="addresscolor__". But the problem is that there is more than one class with this name. And I want to know is there any way to get this data from a specific class with that value, using "Start with" or "Contained.." or some thing like this ?

HTML Code :

<p class="addresscolor__">
    <span>Phone:</span>
    33875362 ، 22356341
    <br />
</p>

<p class="addresscolor__">
    <span>mobile:</span>
    09125231827
    <br />
</p>

I use thhis code:

HtmlNodeCollection nodes2 = doc.DocumentNode.SelectNodes("//p[@class='addresscolor__']");

The other thing is, that this class contained span tag and with these codes I catch the whole <p> tag values, but I want a number only in "Mobile" span tag.

Accepted Answer

For finding this paragraph:

//p[@class='addresscolor__' and contains(span, 'mobile:')]

For limiting to its text node (the phone number):

//p[@class='addresscolor__' and contains(span, 'mobile:')]/text()

For further stripping whitespace:

normalize-space(//p[@class='addresscolor__' and contains(span, 'mobile:')]/text())

For matching HTML classes, also consider a node can have multiple classes and how to deal with that.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow