2017-06-15 41 views
0

我正在使用HtmlAgilityPack和大學我有任務獲取所有鏈接,位於單詞「源」和相關。我試着用這樣的代碼:獲取鏈接搜索詞組

foreach (HtmlNode link in document.DocumentNode.SelectNodes(".//a[@href]")) 
{ 
    if (document.DocumentNode.InnerHtml.ToString().Contains(sourcesDictionary[i])) 
    { 
     string hrefValue = link.GetAttributeValue("href", string.Empty); 
     Console.WriteLine(hrefValue); 
    } 
} 

但它只是打印HTML文檔的所有鏈接。我可以改變什麼才能正常工作?

回答

0

添加突出顯示的行可以幫助

foreach (HtmlNode link in document.DocumentNode.SelectNodes(".//a[@href]")) 
{ 
**if(link.ParentNode.InnerHtml.Contains("source")** 
{ 
if (document.DocumentNode.InnerHtml.ToString().Contains(sourcesDictionary[i])) 
{ 
    string hrefValue = link.GetAttributeValue("href", string.Empty); 
    Console.WriteLine(hrefValue); 
} 
} 
} 
+0

謝謝,它的工作完美! – zxhouse