Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C语言的HTML代码中获取带有特定单词的链接#_C#_Regex_Html Agility Pack - Fatal编程技术网

C# 从C语言的HTML代码中获取带有特定单词的链接#

C# 从C语言的HTML代码中获取带有特定单词的链接#,c#,regex,html-agility-pack,C#,Regex,Html Agility Pack,我正在尝试解析一个网站。我需要在HTML文件,其中包含一些特定的单词一些链接。我知道如何查找“href”属性,但我不需要所有属性,是否有必要这样做?例如,我可以在HtmlAgilityPack中使用正则表达式吗 HtmlNode links = document.DocumentNode.SelectSingleNode("//*[@id='navigation']/div/ul"); foreach (HtmlNode urls in document.DocumentNode.Select

我正在尝试解析一个网站。我需要在HTML文件,其中包含一些特定的单词一些链接。我知道如何查找“href”属性,但我不需要所有属性,是否有必要这样做?例如,我可以在HtmlAgilityPack中使用正则表达式吗

HtmlNode links = document.DocumentNode.SelectSingleNode("//*[@id='navigation']/div/ul");

foreach (HtmlNode urls in document.DocumentNode.SelectNodes("//a[@]"))
{
    this.dgvurl.Rows.Add(urls.Attributes["href"].Value);
}   

我试着用这个来查找HTML代码中的所有链接。

我发现这个和那个对我都有用

HtmlNode links = document.DocumentNode.SelectSingleNode("//*[@id='navigation']/div/ul");
    foreach (HtmlNode urls in document.DocumentNode.SelectNodes("//a[@]"))
        {
           var temp = catagory.Attributes["href"].Value;
           if (temp.Contains("some_word"))
              {
                dgv.Rows.Add(temp);
              }
        }

如果您有如下HTML文件:

<div class="a">
    <a href="http://www.website.com/"></a>
    <a href="http://www.website.com/notfound"></a>
    <a href="http://www.website.com/theword"></a>
    <a href="http://www.website.com/sub/theword"></a>
    <a href="http://www.website.com/theword.html"></a>
    <a href="http://www.website.com/other"></a>
</div>
Regex regex = new Regex("(theworld|other)", RegexOptions.IgnoreCase);

HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='a']");
List<HtmlNode> nodeList = node.SelectNodes(".//a").Where(a => regex.IsMatch(a.Attributes["href"].Value)).ToList<HtmlNode>();

List<string> urls = new List<string>();

foreach (HtmlNode n in nodeList)
{
    urls.Add(n.Attributes["href"].Value);
}
node.SelectNodes(".//a[matches(@href,'(theword|other)')]")
请注意,有一个带有XPATH的
包含
关键字,但您必须为正在搜索的每个单词复制条件,如下所示:

node.SelectNodes(".//a[contains(@href,'theword') or contains(@href,'other')]")
XPATH还有一个
匹配
关键字,不幸的是,它仅适用于XPath2.0,而HtmlAgilityPack使用XPath1.0。使用XPATH 2.0,您可以执行以下操作:

<div class="a">
    <a href="http://www.website.com/"></a>
    <a href="http://www.website.com/notfound"></a>
    <a href="http://www.website.com/theword"></a>
    <a href="http://www.website.com/sub/theword"></a>
    <a href="http://www.website.com/theword.html"></a>
    <a href="http://www.website.com/other"></a>
</div>
Regex regex = new Regex("(theworld|other)", RegexOptions.IgnoreCase);

HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='a']");
List<HtmlNode> nodeList = node.SelectNodes(".//a").Where(a => regex.IsMatch(a.Attributes["href"].Value)).ToList<HtmlNode>();

List<string> urls = new List<string>();

foreach (HtmlNode n in nodeList)
{
    urls.Add(n.Attributes["href"].Value);
}
node.SelectNodes(".//a[matches(@href,'(theword|other)')]")

HtmlNode links=…”的目的是什么?是否应删除该声明?