如何从字符串中获取两个标记之间的值
本文关键字:两个 之间 字符串 获取 | 更新日期: 2023-09-27 17:59:57
我正试图从网页中获取数据。我已将网页下载到一个字符串变量中。
我想知道如何获取两个标签之间的值。我已经包含了下载字符串的一个片段,我想要的值是895
<div class="split2r right">
<strong>Avg. asking rent in M4:</strong>
<strong class="price big">£897 pcm</strong><br>
<strong>No. of properties to rent in M4:</strong> <strong><a data-ga-category="Area stats" data-ga-action="properties_to_rent" data-ga-label="/tracking/home-values/results/" href="/to-rent/property/manchester/isaac-way/m4-7ed/">225</a></strong>
</div>
一个代码示例会很好。
使用HtmlAgilityPack库解析HTML实际上非常容易。
第一步是添加对HtmlAgilityPack
库的引用。然后您可以开始解析HTML:
const string Html = "<strong>Avg. price:</strong> <strong class='"price big'">£895 pcm</strong><br><strong>this is the price of zed headphones</strong>";
var doc = new HtmlDocument();
doc.LoadHtml(Html);
下一步是找到您要查找的元素,在本例中是<strong>
元素,其class
设置为price big
:
var priceNode = doc.DocumentNode.SelectSingleNode("//strong[@class='price big']");
现在,我们的最后一步是从节点的InnerText
属性中检索实际数字。可能最好的方法是通过正则表达式来实现,如果我们假设所需的数字是节点内部文本中的唯一数字,那么这可能非常简单:
var priceMatch = Regex.Match(priceNode.InnerText, @"('d+)");
Console.WriteLine(priceMatch); // Will output 895
private void button1_Click(object sender, EventArgs e)
{
string input = @"<strong class=""price big"">£895 pcm</strong><br>";
MatchCollection mc = Regex.Matches(input, ">£'d{0-5} pcm");
foreach (Match m in mc)
{
Add To List Convert.ToInt32(m);
}
}
假设您的字符串值被称为"source",并且所有提取都被格式化为示例
var value = Regex.Replace(source, @"'D", string.Empty);