简体   繁体   中英

HtmlAgilityPack can't find element

I need to parse a site and I know where to find the element I'm searching: it's a span with class="metadata_with_icon-tags-primary_tag" .

My C# code:

var page = new HtmlWeb().Load(url).DocumentNode.Descendants("span").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("metadata_with_icon-tags-primary_tag"));

Item that I need: 在此处输入图片说明

Try this

HtmlWeb website = new HtmlWeb();
            var html = website.Load("https://genius.com/Eminem-space-bound-lyrics").DocumentNode.InnerHtml;  

            Regex rgx = new Regex(@"<script\b[^>]*>([\s\S]*?)<\/script>", RegexOptions.IgnoreCase);
            var matches = rgx.Matches(html);
            var g = matches[14].Value;

            Regex regex = new Regex(
                @"(\[{.*}\])",
                RegexOptions.Multiline
            );

            Match match = regex.Match(g);
            var json = match.Value;

要使用class="metadata_with_icon-tags-primary_tag"获取span ,请执行以下操作:

HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='metadata_with_icon-tags-primary_tag']");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM