简体   繁体   English

如何使用HTMLagility在具有某些类属性的两个div标签之间获取文本

[英]How to get text between two div tags with some class attribute with HTMLagility

I want to get some text from two html div from HTML file. 我想从HTML文件的两个html div中获取一些文本。 After some searches i decided to use HTMLAgility Pack for doing this. 经过一些搜索后,我决定使用HTMLAgility Pack来执行此操作。 I wrote this code : 我写了这段代码:

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(result);
    HtmlNode node = doc.DocumentNode.SelectSingleNode("//*div[@class='item']"); 
    string value = node.InnerText; 

'result' is my content of the File. “结果”是我在文件中的内容。 But i get this exception : 'Expression must evaluate to a node-set' 但是我遇到了一个例外:“表达式必须计算为一个节点集”

And this is some of mt file's content : 这是mt文件的一些内容:

<div class="Clear" style="height:15px;"></div>
<div class='Container Select' id="Container_1">
<div class='Item'><div class='Part Lable'>موضوع : </div><div class='Part ...

try either 尝试之一

"//*/div[@class='item']"

or simply 或简单地

"//div[@class='item']"

have you tried using XPath for example if I wanated to find a if a node is selected in my example I would do the following 您是否尝试过使用XPath例如,如果我想查找示例中是否选择了节点,我将执行以下操作

string xpath = null;
XmlNode configNode = configDom.DocumentElement;
// collect selected nodes in node list
XmlNodeList nodeList =
configNode.SelectNodes(@"//*[@status='checked']");

in your case you would do the following 在您的情况下,您将执行以下操作

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(result);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//*/div[@class='item']"); 
string value = node.InnerText; 

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

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