Beispiel #1
0
        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);
        }
Beispiel #2
0
        public void DoNotFixAllRelativeHrefs()
        {
            var selectable = new HtmlSelectable("<div><a href=\"aaaa.com\">aaaaaaab</a></div>");
            var values     = selectable.SelectList(Selectors.XPath(".//a")).ToArray();

            Assert.Equal("aaaaaaab", values.First().Value);
        }
Beispiel #3
0
        public void RemoveOutboundLinks()
        {
            // 绝对路径不需要做补充
            var selectable2 = new HtmlSelectable("<div><a href=\"http://www.aaaa.com\">aaaaaaab</a></div>",
                                                 "http://www.b.com");
            var value2 = selectable2.SelectList(Selectors.XPath(".//a"));

            Assert.Null(value2);
        }
Beispiel #4
0
        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]);
        }