找到同一个班级的第二个div(Find second div with same class)

试图在页面上找到具有相同类的第二个div。 我只在获取数据时检索第一个,并且无法弄清楚如何获取第二个或第三个等。

HtmlAgilityPack.HtmlDocument data = web.Load(URL); var res = data.DocumentNode.SelectSingleNode("//div[@class='col-sm-5']");

另外我在开始时使用两个斜线标志,我不知道为什么但它有效。 我见过很多不同的解决方案("/", "./" "//" ".//"). 有人可以解释一下这个区别吗?

提前谢谢,xolo

Trying to find second div with same class on a page. I only retrieve the first one when fetching the data and cannot figure out how to get the second or third etc..

HtmlAgilityPack.HtmlDocument data = web.Load(URL); var res = data.DocumentNode.SelectSingleNode("//div[@class='col-sm-5']");

Also I'm using two slash signs in the start, I don't know why but it worked. I've seen numerous of different solutions ("/", "./" "//" ".//"). Could someone explain the difference please?

Thanks in advance, xolo

最满意答案

试试这个命令:

var res = data.DocumentNode.SelectNodes("//div[@class='col-sm-5']");

这是单斜杠和双斜杠之间的区别:

/

从文档节点开始选择 允许您创建“绝对”路径表达式 例如“/ html / body / p”匹配所有段落元素

//

在文档中的任何位置开始选择匹配 允许您创建“相对”路径表达式 例如“// p”匹配所有段落元素

Try this command:

var res = data.DocumentNode.SelectNodes("//div[@class='col-sm-5']");

This is the difference between single and double slash:

/

start selection from the document node allows you to create 'absolute' path expressions e.g. “/html/body/p” matches all the paragraph elements

//

start selection matching anywhere in the docume allows you to create 'relative' path expressions e.g. “//p” matches all the paragraph elements

更多推荐