本文整理汇总了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);
}
示例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);
}
示例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));
}
示例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 "";
}
}
示例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 "";
}
}
示例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;
}
}
示例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");
}
示例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 "";
}
}
示例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 "";
}
}