简体   繁体   English

使用C#从HTML解析值

[英]Parse value out of HTML using C#

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. 该HTML有数千行。 This mark-up has the same format always. 此标记始终具有相同的格式。 The price can change only. 价格只能改变。

I've tried parsing the HMTL using HtmlAgilityPack . 我尝试使用HtmlAgilityPack解析HMTL。 I didn't succeed. 我没有成功。 Since the HTML format is always the same, there must be a better way to obtain the price value. 由于HTML格式始终相同,因此必须有更好的方法来获取价格值。

Can you give me suggestions about how to parse an HTML? 您能给我有关如何解析HTML的建议吗?

Use an XPath expression: 使用XPath表达式:

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

where document is an HtmlElement/HtmlDocument.DocumentNode . 其中documentHtmlElement/HtmlDocument.DocumentNode

String x= webbrowser.Document.getelementsbytagname("span")[0].innerHtml

您需要在ac#中打开网页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM