serhiy1994
serhiy1994

Reputation: 59

HtmlAgilityPack - get the second class with the same name correctly

I want to parse some Pikabu.ru pages and now I need to get a user's total posts number. For example, it's my own profile. I need to get the '280' number. https://api.asm.skype.com/v1/objects/0-weu-d6-606eeb4b94e49a4ef8971bec5767e1b0/views/imgpsh_fullsize_anim

As you can see, there are 4 elements with 'profile__digital' class, so I need to parse only the third one.

I wrote this:

HtmlNode node = (doc.DocumentNode.Descendants("section").Where(d => d.Attributes["class"].Value.Contains("section_padding_none")).First()); //all is ok
textBox2.Text = node.SelectSingleNode("//span[contains(@class, 'profile__digital')][2]").InnerHtml; //wrong!

But it throws ArgumentOutOfRange Exception((. How to parse the needed data correctly? Please help. Thank you.

Upvotes: 0

Views: 221

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134881

I don't think using section_padding_none as an anchor for your queries is a good one. I think using profile__section would be a better choice. First step is to narrow down the sections to the ones you want (in this case, it's the second of three).

//div[contains(@class,'profile__section')][2]
<div class="profile__section">
          <span class="profile__digital hint" aria-label="30&emsp14;685"><b>30К</b> <span><span>рейтинг</span></span></span>
          <span class="profile__digital"><b>2161</b> <span>подписчик</span></span>
          <span class="profile__digital"><b>1940</b> <span>комментариев</span></span>
          <span class="profile__digital"><b>280</b> <span>постов</span></span>
          <span class="profile__digital"><b>103</b> <span>в "горячем"</span></span>
        </div>

The section is organized using spans with the value bolded. So select the span with the value you want (the fourth) then the bold tag.

span[contains(@class,'profile__digital')][4]/b

This could all be combined a single xpath expression.

//div[contains(@class,'profile__section')][2]/span[contains(@class,'profile__digital')][4]/b

Upvotes: 1

Related Questions