Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取两个html标记之间的文本#_C#_Html_String_Tags - Fatal编程技术网

C# 获取两个html标记之间的文本#

C# 获取两个html标记之间的文本#,c#,html,string,tags,C#,Html,String,Tags,我试图在提供的html(span)之间获取数据(在本例中为31) 以下是原始代码(来自chrome中的inspect elements) 31 我有一个富文本框,其中包含页面的源代码,下面是相同的代码,但在富文本框的第51行: 您有17个积分 我该怎么做呢?我试过几种方法,但似乎没有一种对我有效 我正在尝试从此页面检索点值: 这个数字会根据你的下属而变化。有多种可能性 让HTML被解析为XML,并通过 遍历所有元素。如果使用span标记,请跳过所有字符,直到找到结束“>”。那么你需要的价值

我试图在提供的html(span)之间获取数据(在本例中为31)

以下是原始代码(来自chrome中的inspect elements)

31
我有一个富文本框,其中包含页面的源代码,下面是相同的代码,但在富文本框的第51行:

您有
17个
积分
我该怎么做呢?我试过几种方法,但似乎没有一种对我有效

我正在尝试从此页面检索点值:
这个数字会根据你的下属而变化。

有多种可能性

  • 让HTML被解析为XML,并通过

  • 遍历所有元素。如果使用span标记,请跳过所有字符,直到找到结束“>”。那么你需要的价值就是在下一个开场前的一切,你可以非常具体地说:

    var regex = new Regex(@"<span id=""point_total"" class=""tooltip"" oldtitle="".*?"" aria-describedby=""ui-tooltip-0"">(.*?)</span>");
    
    var match = regex.Match(@"<span id=""point_total"" class=""tooltip"" oldtitle=""Note: If the number is black, your points are actually a little bit negative.  Don't worry, this just means you need to start subbing again."" aria-describedby=""ui-tooltip-0"">31</span>");
    
    var result = match.Groups[1].Value;
    
    var regex=new regex(@“(.*)”;
    var match=regex.match(@“您需要使用它来完成此操作,它非常简单:

    HtmlDocument doc = new HtmlDocument();
    doc.Load("filepath");
    
    HtmlNode node = doc.DocumentNode.SelectSingleNode("//span"); //Here, you can also do something like (".//span[@id='point_total' class='tooltip' jQuery16207621750175125325='23' oldtitle='Note: If the number is black, your points are actually a little bit negative.  Don't worry, this just means you need to start subbing again.']"); to select specific spans, etc...
    
    string value = node.InnerText; //this string will contain the value of span, i.e. <span>***value***</span>
    
    HtmlDocument doc=新的HtmlDocument();
    文件加载(“文件路径”);
    HtmlNode node=doc.DocumentNode.SelectSingleNode(//span”);//在这里,您还可以执行类似的操作(“.//span[@id='point\u total'class='tooltip'jQuery16207621750175125325='23'oldtitle='注意:如果数字是黑色的,您的分数实际上有点负。别担心,这只是意味着您需要再次开始subbing.]”);选择特定跨度等。。。
    string value=node.InnerText;//此字符串将包含span的值,即.**value***
    
    Regex虽然是一个可行的选项,但如果在解析html时可能的话,您通常希望避免使用它(请参阅)


    就可持续性而言,您需要确保了解页面来源(即,刷新几次,看看每次刷新后您的目标范围是否嵌套在相同的父级中,确保页面采用相同的常规格式,等等,然后使用上述原则导航到该范围)。

    您可以添加一个“runat=server”“如果需要在代码隐藏中访问,请访问span并获取内部文本。jquery解决方案可以吗?var yourdata=$('span').html();我写的程序是用C#编写的,因为我对它相当陌生。请你解释一下你所说的jquery解决方案是什么意思吗?我尝试了一些我在网上找到的正则表达式方法,我还尝试了使用Htmlagibility库来查找字符串。你试图在哪里访问这些数据?在C#代码中的codebehind中,还是在客户端主动地?这除了一件我应该在原始帖子中提到的事情之外,它的值会发生变化,所以它并不总是等于31。这段代码对我来说是有效的,除了它保持显示相同的数字,不管实际值是多少。你确定要重新加载页面源吗?是的,我设置了一个计时器,每5分钟刷新一次源代码第二,感谢您将我重定向回HTML Agility Pack,这是我过去使用过的。我刚刚添加了引用,并在我自己的项目中使用了您的代码作为起点。我唯一想为其他后来者添加的是确保并指定类型以防止歧义。我注意到“.Load()'将不会编译,直到我更改为'HtmlAgilityPack.HtmlDocument doc=new HtmlAgilityPack.HtmlDocument();'
    var regex = new Regex(@"<span id=""point_total"" class=""tooltip"" oldtitle="".*?"" aria-describedby=""ui-tooltip-0"">(.*?)</span>");
    
    var match = regex.Match(@"<span id=""point_total"" class=""tooltip"" oldtitle=""Note: If the number is black, your points are actually a little bit negative.  Don't worry, this just means you need to start subbing again."" aria-describedby=""ui-tooltip-0"">31</span>");
    
    var result = match.Groups[1].Value;
    
    HtmlDocument doc = new HtmlDocument();
    doc.Load("filepath");
    
    HtmlNode node = doc.DocumentNode.SelectSingleNode("//span"); //Here, you can also do something like (".//span[@id='point_total' class='tooltip' jQuery16207621750175125325='23' oldtitle='Note: If the number is black, your points are actually a little bit negative.  Don't worry, this just means you need to start subbing again.']"); to select specific spans, etc...
    
    string value = node.InnerText; //this string will contain the value of span, i.e. <span>***value***</span>