public void FixRelativeUrl() { var absoluteUrl = UriUtils.CanonicalizeUrl("?aa", "http://www.dianping.com/sh/ss/com"); Assert.Equal("http://www.dianping.com/sh/ss/com?aa", absoluteUrl); absoluteUrl = UriUtils.CanonicalizeUrl("../aa", "http://www.dianping.com/sh/ss/com"); Assert.Equal("http://www.dianping.com/sh/aa", absoluteUrl); absoluteUrl = UriUtils.CanonicalizeUrl("..aa", "http://www.dianping.com/sh/ss/com"); Assert.Equal("http://www.dianping.com/sh/ss/..aa", absoluteUrl); absoluteUrl = UriUtils.CanonicalizeUrl("../../aa", "http://www.dianping.com/sh/ss/com/"); Assert.Equal("http://www.dianping.com/sh/aa", absoluteUrl); absoluteUrl = UriUtils.CanonicalizeUrl("../../aa", "http://www.dianping.com/sh/ss/com"); Assert.Equal("http://www.dianping.com/aa", absoluteUrl); // 只有相对路径需要做补充 var selectable1 = new HtmlSelectable("<div><a href=\"/a/b\">aaaaaaab</a></div>", "http://www.b.com"); var value1 = selectable1.Links().First(); Assert.Equal("http://www.b.com/a/b", value1); // 绝对路径不需要做补充 var selectable2 = new HtmlSelectable("<div><a href=\"http://www.aaaa.com\">aaaaaaab</a></div>", "http://www.b.com", false); var value2 = selectable2.SelectList(Selectors.XPath(".//a")).First().Value; Assert.Equal("aaaaaaab", value2); }
public void XPath2() { var selectable = new HtmlSelectable(html2); var links1 = selectable.Links(); var divs = selectable.SelectList(Selectors.XPath("//div")).ToList(); var link2 = divs[0].Links().ToList(); var link3 = divs[1].Links().ToList(); Assert.Equal(2, links1.Count()); Assert.Single(link2); Assert.Single(link3); Assert.Equal("http://whatever.com/aaa", link2[0]); Assert.Equal("http://whatever.com/bbb", link3[0]); }