简体   繁体   English

使用HTML Agility Pack在HTML文档中按类名称获取Element

[英]Get Element by class name in Html document with Html Agility Pack

I want to get an element by class name with html agility pack.the element is inside a lot of other elements, so i can't get element like: 我想通过html agility pack通过类名获取一个元素。该元素在很多其他元素内部,所以我无法获得像这样的元素:

//section[@class='class1']/div[@class='class2']/div[@class='class3']/

I Want to get my element <p class="title">My Text</p> directly. 我想直接获取元素<p class="title">My Text</p>

I get the body of html document with the following code: 我用以下代码获取html文档的主体:

var body = htmlDoc.DocumentNode.SelectSingleNode("//body");

Simply, try this code: 只需尝试以下代码:

HtmlNode myParagraph = htmlDoc.DocumentNode.SelectSingleNode("//p[@class='title']");

I hope this helps you. 我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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