简体   繁体   English

C#-使用HTMLAgility提取特定的div类文本

[英]C# - Extract speciifc div class text using HTMLAgility

I have a code in C# where I want to extract the below value (the text "I want this text" in the HTML code below). 我在C#中有一个代码想要提取以下值(以下HTML代码中的文本“我想要此文本”)。 I have reformat the HTML code to make it easily readable. 我已经重新格式化了HTML代码以使其易于阅读。

<div class="paste-copy-url" style="margin:0 0 0 0;">
    <h4>My Stats:</h4>
    <div class="line">
        <div class="wrap-input">
            <input onclick="this.select();" value="I want this text" readonly="readonly">
        </div>
    </div>
    <h4>Website Link:</h4>
    <div class="line">
        <div class="wrap-input"><input onclick="this.select();" value="Some value" readonly="readonly">
        </div>
    </div>
</div>

The code I tried (It is giving me the text : "Website Link:"): 我尝试过的代码(它给了我文本:“网站链接:”):

var myvaluetoextract = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='paste-copy-url']");

What am I doing wrong? 我究竟做错了什么? Can I use this approach to get that element (There is only 1 instance of the div class in the page)? 我可以使用这种方法来获取该元素吗(页面中div类只有1个实例)?

var input = htmlDocument.DocumentNode
           .SelectSingleNode("//div[@class='paste-copy-url']//div[@class='wrap-input']/input");
var yourText = input.Attributes["value"].Value;

You can do it like this: 您可以这样做:

var myvaluetoextract = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='paste-copy-url']//input");
var value = myvaluetoextract.GetAttributeValue("value", null);

//input means you search for input elements in the div 's subtree, recursively. //input表示您递归地在div的子树中搜索input元素。 GetAttributeValue is a helper that will never fail, even if the attribute doesn't exists (in this case if will return the 2nd passed parameter - which is null here) GetAttributeValue是一个永远不会失败的帮助器,即使该属性不存在(在这种情况下,如果将返回第二个传递的参数-此处为null

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

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