如何验证网页上是否存在超链接?

发布于 2024-09-18 15:59:40 字数 1433 浏览 1 评论 0原文

我需要验证给定网页上是否存在特定的超链接。我知道如何下载 HTML 源代码。我需要帮助的是确定“目标”网址是否作为“源”网页中的超链接存在。

这是一个演示该问题的小控制台程序:

public static void Main()
{
    var sourceUrl = "http://developer.yahoo.com/search/web/V1/webSearch.html";
    var targetUrl = "http://developer.yahoo.com/ypatterns/";
    Console.WriteLine("Source contains link to target? Answer = {0}",
                      SourceContainsLinkToTarget(
                          sourceUrl,
                          targetUrl));
    Console.ReadKey();
}

private static bool SourceContainsLinkToTarget(string sourceUrl, string targetUrl)
{
    string content;
    using (var wc = new WebClient())
        content = wc.DownloadString(sourceUrl);
    return content.Contains(targetUrl); // Need to ensure this is in a <href> tag!
}

注意最后一行的注释。我可以查看目标 URL 是否存在于源 URL 的 HTML 中,但我需要验证 URL 是否位于 标记内。这样我就可以验证它实际上是一个超链接,而不仅仅是文本。

我希望有人能有一个强大的正则表达式或我可以使用的东西。

谢谢!


这是使用 HtmlAgilityPack 的解决方案:

   private static bool SourceContainsLinkToTarget(string sourceUrl, string targetUrl)
    {
        var doc = (new HtmlWeb()).Load(sourceUrl);
        foreach (var link in doc.DocumentNode.SelectNodes("//a[@href]"))
            if (link.GetAttributeValue("href",
                                       string.Empty).Equals(targetUrl))
                return true;
        return false;
    }

I have a need to verify a specific hyperlink exists on a given web page. I know how to download the source HTML. What I need help with is figuring out if a "target" url exists as a hyperlink in the "source" web page.

Here is a little console program to demonstrate the problem:

public static void Main()
{
    var sourceUrl = "http://developer.yahoo.com/search/web/V1/webSearch.html";
    var targetUrl = "http://developer.yahoo.com/ypatterns/";
    Console.WriteLine("Source contains link to target? Answer = {0}",
                      SourceContainsLinkToTarget(
                          sourceUrl,
                          targetUrl));
    Console.ReadKey();
}

private static bool SourceContainsLinkToTarget(string sourceUrl, string targetUrl)
{
    string content;
    using (var wc = new WebClient())
        content = wc.DownloadString(sourceUrl);
    return content.Contains(targetUrl); // Need to ensure this is in a <href> tag!
}

Notice the comment on the last line. I can see if the target URL exists in the HTML of the source URL, but I need to verify that URL is inside of a <href/> tag. This way I can validate it's actually a hyperlink, instead of just text.

I'm hoping someone will have a kick-ass regular expression or something I can use.

Thanks!


Here is the solution using the HtmlAgilityPack:

   private static bool SourceContainsLinkToTarget(string sourceUrl, string targetUrl)
    {
        var doc = (new HtmlWeb()).Load(sourceUrl);
        foreach (var link in doc.DocumentNode.SelectNodes("//a[@href]"))
            if (link.GetAttributeValue("href",
                                       string.Empty).Equals(targetUrl))
                return true;
        return false;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2024-09-25 15:59:40

最好的方法是使用带有内置 DOM 解析器的网页抓取库,它将根据 HTML 构建对象树,并让您以编程方式探索它以查找您要查找的链接实体。有很多可用的 - 例如 Beautiful Soup (python) 或 scrapi (ruby) 或 Mechanize (perl)。对于 .net,请尝试 HTML 敏捷包。 http://htmlagilitypack.codeplex.com/

The best way is to use a web scraping library with a built in DOM parser, which will build an object tree out of the HTML and let you explore it programmatically for the link entity you are looking for. There are many available - for example Beautiful Soup (python) or scrapi (ruby) or Mechanize (perl). For .net, try the HTML agility pack. http://htmlagilitypack.codeplex.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文