如何使用HtmlAgilityPack提取完整的URL - C# [英] How to extract full url with HtmlAgilityPack - C#

查看:1059
本文介绍了如何使用HtmlAgilityPack提取完整的URL - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,下面的方法只提取这样的引用网址

Alright with the way below it is extracting only referring url like this

提取代码:

foreach (HtmlNode link in hdDoc.DocumentNode.SelectNodes("//a[@href]"))
{
    lsLinks.Add(link.Attributes["href"].Value.ToString());
}

网址代码

<a href="Login.aspx">Login</a>

提取的网址

Login.aspx

但我希望得到真正的链接,解析浏览器是什么颜色

But i want to get real link what browser parsed like

http://www.monstermmorpg.com/Login.aspx

我可以检查网址是否包含http,如果没有添加域值,但在某些情况下可能会导致一些问题,我认为这不是一个非常明智的解决方案。

I can do it with checking the url whether containing http and if not add the domain value but it may cause some problems at some occasions and i think not a very wise solution.

c#4.0,HtmlAgilityPack.1.4.0

c# 4.0 , HtmlAgilityPack.1.4.0

推荐答案

假设你拥有原始网址,您可以将解析后的网址组合成以下内容:

Assuming you have the original url, you can combine the parsed url something like this:

// The address of the page you crawled
var baseUrl = new Uri("http://example.com/path/to-page/here.aspx");

// root relative
var url = new Uri(baseUrl, "/Login.aspx");
Console.WriteLine (url.AbsoluteUri); // prints 'http://example.com/Logon.aspx'

// relative
url = new Uri(baseUrl, "../foo.aspx?q=1");
Console.WriteLine (url.AbsoluteUri); // prints 'http://example.com/path/foo.aspx?q=1'

// absolute
url = new Uri(baseUrl, "http://stackoverflow.com/questions/7760286/");
Console.WriteLine (url.AbsoluteUri); // prints 'http://stackoverflow.com/questions/7760286/'

// other...
url = new Uri(baseUrl, "javascript:void(0)");
Console.WriteLine (url.AbsoluteUri); // prints 'javascript:void(0)'

注意使用 AbsoluteUri 并且不依赖于 ToString()因为 ToString 解码URL(使其成为更人性化),通常是你想要的。

Note the use of AbsoluteUri and not relying on ToString() because ToString decodes the URL (to make it more "human-readable"), which is not typically what you want.

这篇关于如何使用HtmlAgilityPack提取完整的URL - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆