如何将标签中的信息转换为c#和HTMLAgilityPack?



我想获得一些信息没有c# htmllagilitypack标签。(示例:<(a) href = "https://hashcode.co.kr"description = "…>)我想要得到href的值)

我该怎么做?

HTML敏捷性包具有许多其他解决方案所缺乏的知识,例如,如果您使用正则表达式这样做,它可能会被HTML的一些奇怪之处绊倒。

然而,如果你想这样做,你可以使用表达式:href="(.*)"

笔记……

  1. 如果你有href = "url"
  2. ,这将不起作用
  3. 如果使用单引号,这将不起作用,即href='url'
  4. 这将不适用于许多其他可能的HTML变体,没有引号,制表符而不是空格,缺少空格等

下面是一个c#的例子:

using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
string pattern = @"href=""(.*)"" ";
string input = "An extraordinary day <a href="https://hashcode.co.kr" description="example">dawns</a> with each new day.";
Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
if (m.Success)
Console.WriteLine("Found '{0}' at position {1}.", m.Value, m.Index);
}
}

最新更新