繁体   English   中英

将正则表达式转换为HtmlAgilityPack C#

[英]Regex to HtmlAgilityPack C#

我想知道如何将使用正则表达式的代码转换为与使用HtmlAgilityPack库的其他网站中的字符串匹配的代码。

示例代码:

<div class="element"><div class="title"><a href="127.0.0.1" title="A.1">A.1</a></div></div>
<div class="element"><div class="title"><a href="127.0.0.1" title="A.2">A.2</a></div></div>

我当前的代码如下:

List<string> Cap = new List<string>();
WebClient web = new WebClient();
string url = web.DownloadString("127.0.0.1");
MatchCollection cap = Regex.Matches(url, "title=\"(.+?)\">", RegexOptions.Singleline);
foreach (Match m in cap)
{
     Cap.Add(m.Groups[1].Value.ToString());
}
lst_Cap.ItemsSource = Cap;

而且有效。

我已经尝试过使用HtmlAgilityPack:

HtmlDocument Web = web.Load("127.0.0.1"); // 127.0.0.1 for example
List<string> Cap = new List<string>();
foreach (HtmlNode node in Web.DocumentNode.SelectNodes("//*[@id=\"content\"]/div/div[3]/div[2]/div[1]/a"))
{
    Cap.Add(node.InnerHtml);
}

但它仅添加A.1。

我能怎么做?

您的正则表达式"title=\\"(.+?)\\">"匹配并捕获HTML文档内任何标签中的任何title属性。

因此,使用另一个带有//*[@title] XPath的代码,该代码获取包含title属性的任何元素节点( * ),然后仅遍历属性节点,一旦其名称为title ,则将值添加到列表中:

var nodes = Web.DocumentNode.SelectNodes("//*[@title]");
if (nodes != null)
{
   foreach (var node in nodes)
   {
       foreach (var attribute in node.Attributes)
           if (attribute.Name == "title")
               Cap.Add(attribute.Value);
   }
}

或使用LINQ:

var nodes = Web.DocumentNode.SelectNodes("//*[@title]");
var res = nodes.Where(p => p.HasAttributes)
                 .Select(m => m.GetAttributeValue("title", string.Empty))
                 .Where(l => !string.IsNullOrEmpty(l))
                 .ToList();

暂无
暂无

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

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