当前位置: 首页>>代码示例>>C#>>正文


C# HtmlDocument.QuerySelector方法代码示例

本文整理汇总了C#中HtmlAgilityPack.HtmlDocument.QuerySelector方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlDocument.QuerySelector方法的具体用法?C# HtmlDocument.QuerySelector怎么用?C# HtmlDocument.QuerySelector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HtmlAgilityPack.HtmlDocument的用法示例。


在下文中一共展示了HtmlDocument.QuerySelector方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Parse

        public static ComicParseResult Parse(string html)
        {
            if (html == null) throw new ArgumentNullException(nameof(html));
            if (html == "") throw new ArgumentException($"{nameof(html)} should contain some text", nameof(html));

            var doc = new HtmlDocument();
            doc.LoadHtml(html);

            var img = doc.QuerySelectorAll("img.strip")
                .FirstOrDefault(s => !s.Attributes.Contains("width"));
            if (img == null)
                return ComicParseResult.Fail("Could not find strip url");

            var src = img.GetAttributeValue("src", null);
            if (src == null)
                return ComicParseResult.Fail("img element did not have src attribute");

            var meta = doc.QuerySelector("meta[name='twitter:url']");
            if (meta == null)
                return ComicParseResult.Fail("Could not load twitter:url attribtue");

            var content = meta.GetAttributeValue("content", null);
            if (content == null)
                return ComicParseResult.Fail("meta tag did not have content attribute");

            var dateString = content.Replace("http://www.gocomics.com/pearlsbeforeswine/", "");

            var date = DateTime.ParseExact(dateString, "yyyy/MM/dd", CultureInfo.InvariantCulture);
            var uri = new Uri(src);

            return ComicParseResult.Succeed(uri, date);
        }
开发者ID:akatakritos,项目名称:Comics,代码行数:32,代码来源:PearlsParser.cs

示例2: Parse

        public static ComicParseResult Parse(string html)
        {
            if (html == null)
                throw new ArgumentNullException(nameof(html));

            if (string.IsNullOrEmpty(html))
                throw new ArgumentException("html is empty", nameof(html));

            var doc = new HtmlDocument();
            doc.LoadHtml(html);

            var image = doc.GetElementbyId("main-comic");
            if (image == null)
            {
                return ComicParseResult.Fail("Could not find comic img element");
            }

            var dateNode = doc.QuerySelector(".past-week-comic-title");
            if (dateNode == null)
            {
                return ComicParseResult.Fail("Could not find date elemenet");
            }

            DateTime date;
            if (!DateTime.TryParseExact(dateNode.InnerText, "yyyy.MM.dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
            {
                return ComicParseResult.Fail($"Could not parse date: '{dateNode.InnerText}'");
            }

            var src = EnsureHttp(image.Attributes["src"].Value);

            return ComicParseResult.Succeed(src, date);
        }
开发者ID:akatakritos,项目名称:Comics,代码行数:33,代码来源:ExplosmParser.cs

示例3: Parse

        public static ComicParseResult Parse(string html)
        {
            if (html == null) throw new ArgumentNullException(nameof(html));
            if (html == "") throw new ArgumentException("html must have some contents", nameof(html));

            var doc = new HtmlDocument();
            doc.LoadHtml(html);

            var img = doc.QuerySelector(".img-comic");
            var src = img?.GetAttributeValue("src", null);

            var meta = doc.QuerySelector("meta[property='article:publish_date']");
            var date = meta?.GetAttributeValue("content", null);

            if (src == null)
                return ComicParseResult.Fail("Could not find image src");
            if (date == null)
                return ComicParseResult.Fail("Could not find publish date");

            return ComicParseResult.Succeed(new Uri(src), DateTime.Parse(date));
        }
开发者ID:akatakritos,项目名称:Comics,代码行数:21,代码来源:DilbertParser.cs

示例4: GetCategory

        public string GetCategory(HtmlDocument doc)
        {
            try
            {

                HtmlNode node = doc.QuerySelector("[data-ng-bind~=category]");
                return node.InnerText;
            }
            catch (Exception e)
            {
                _logger.Error("Get Category",e);
                return "";
            }
        }
开发者ID:anandraj204,项目名称:First,代码行数:14,代码来源:ReviewParserNode.cs

示例5: GetStrainUrl

        public string GetStrainUrl(HtmlDocument doc)
        {
            try
            {

                HtmlNode node = doc.QuerySelector("a.strain-link");
                var url = node.Attributes["href"];
                return url.Value;
            }
            catch (Exception e)
            {
                _logger.Error("Get Strain URL", e);
                return "";
            }
        }
开发者ID:anandraj204,项目名称:First,代码行数:15,代码来源:ReviewParserNode.cs

示例6: GetRating

        public decimal GetRating(HtmlDocument doc)
        {
            try
            {

                HtmlNode node = doc.QuerySelector("span[star-rating]");
                var rating = node.Attributes["star-rating"];
                return Decimal.Parse(rating.Value);
            }
            catch (Exception e)
            {
                _logger.Error("Get Rating",e);
                return -1;
            }
        }
开发者ID:anandraj204,项目名称:First,代码行数:15,代码来源:ReviewParserNode.cs

示例7: ContentHasImageTagWithSource

        public void ContentHasImageTagWithSource()
        {
            var feed = new ComicFeed("Foobar", new Uri("http://abc.def"));

            var xml = feed.Render(new[]
            {
                new Comic()
                {
                    Permalink = "http://www.google.com/",
                    ImageSrc = "http://example.com/image.png"
                }
            });

            var rss = ReadRssXml(xml);
            var doc = new HtmlDocument();
            doc.LoadHtml(rss.Items.First().Summary.Text);
            var img = doc.QuerySelector("img");

            Check.That(img.GetAttributeValue("src", null)).IsEqualTo("http://example.com/image.png");
        }
开发者ID:akatakritos,项目名称:Comics,代码行数:20,代码来源:ComicFeedTests.cs

示例8: GetReview

        public string GetReview(HtmlDocument doc)
        {
            try
            {

                HtmlNode node = doc.QuerySelector("div.m-review > div.padding-rowItem--xl .notranslate");
                return node.InnerText;
            }
            catch (Exception e)
            {
                _logger.Error("Get Review", e);
                return "";
            }
        }
开发者ID:anandraj204,项目名称:First,代码行数:14,代码来源:ReviewParserNode.cs

示例9: GetStrainTitle

 public string GetStrainTitle(HtmlDocument doc)
 {
     try{
         HtmlNode node = doc.QuerySelector("[data-ng-bind-html~=name]");
         return node.InnerText;  
     }
     catch (Exception e)
     {
         _logger.Error("Get Strain Title",e);
         return "";
     }
 }
开发者ID:anandraj204,项目名称:First,代码行数:12,代码来源:ReviewParserNode.cs


注:本文中的HtmlAgilityPack.HtmlDocument.QuerySelector方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。