使用搜索短语获取 link
Gets link with searching phrase
我正在使用 HtmlAgilityPack,在大学期间我的任务是获取所有链接,这些链接位于 "source" 和相关词的旁边。我试过这样的代码:
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);
}
}
}
我正在使用 HtmlAgilityPack,在大学期间我的任务是获取所有链接,这些链接位于 "source" 和相关词的旁边。我试过这样的代码:
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);
}
}
}