C# 获取与搜索短语的链接
我使用的是HtmlAgilityPack,在univerity上,我有一个任务,就是获取所有链接,这些链接位于“源”一词旁边,并且是相关的。我试过这样一个代码:C# 获取与搜索短语的链接,c#,search,web-crawler,html-agility-pack,word,C#,Search,Web Crawler,Html Agility Pack,Word,我使用的是HtmlAgilityPack,在univerity上,我有一个任务,就是获取所有链接,这些链接位于“源”一词旁边,并且是相关的。我试过这样一个代码: foreach (HtmlNode link in document.DocumentNode.SelectNodes(".//a[@href]")) { if (document.DocumentNode.InnerHtml.ToString().Contains(sourcesDictionary[i])) {
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文档中的所有链接。我可以更改什么以使其正常工作?添加突出显示的行可能会有所帮助
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);
}
}
}