0

I would like to be able to extract a number from the following mark-up:

<span class="hidden" itemprop="price">17.73</span>

This HTML has thousands of lines. This mark-up has the same format always. The price can change only.

I've tried parsing the HMTL using HtmlAgilityPack. I didn't succeed. Since the HTML format is always the same, there must be a better way to obtain the price value.

Can you give me suggestions about how to parse an HTML?

Sender
  • 6,660
  • 12
  • 47
  • 66
Darin
  • 119
  • 2
  • 12
  • @FᴀʀʜᴀɴAɴᴀᴍ – Sophie Coyne Dec 19 '15 at 11:38
  • It is not clear what you mean with "without success". A HTML parser (like HtmlAgilityPack) seems the best solution for any HTML-related parsing action. In any case, if the target string is always the same you might extract it right away with different in-built methods (e.g., `Split` or `IndexOf`); or just iterate through each element in it until finding what you want (e.g., loop + `Substring`). It is not clear what you want, it is not clear what you have tried and it seems that there are many alternatives to deliver what you are after. This question is offtopic. – varocarbas Dec 19 '15 at 11:43
  • `"17.73".Split(">")[1].Split("<")[0];` could be a start as you said that the string is always the same. – Arthur Rey Dec 19 '15 at 11:47
  • Using a HTML parser is the proper way to do it. – ThiefMaster Dec 19 '15 at 11:47

2 Answers2

0

Use an XPath expression:

string x = document.SelectSingleNode(@"//span[@class='hidden' and @itemprop='price']").innerText;

where document is an HtmlElement/HtmlDocument.DocumentNode.

Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
0
String x= webbrowser.Document.getelementsbytagname("span")[0].innerHtml

You need open a web page in a c#

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
simone989
  • 397
  • 1
  • 3
  • 8