【发布时间】:2026-01-18 14:30:01
【问题描述】:
我已经尝试解析这段代码很长时间了:
<html>
<body class="detailpage">
<div id="innerLayout">
<section id="body-container">
<div class="wrapper">
<div class="content" id="offer_active">
<div class="clr offerbody">
<div class="offercontent fleft rel ">
<div class="offercontentinner">
<script>
texto = {"name":"John"};
</script>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</body>
</html>
我更喜欢使用AgilityPack,因此我想得到"name" : "John",但我没有成功。
这是我的尝试:
string stringThatKeepsYourHtml = @"<!DOCTYPE html> <head> <title>Title</title> </head> <body> <div id=""myId"" class=""myClass""> <div class=""myClass"">hello</div> </div> </body> </html>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(stringThatKeepsYourHtml);
string whatUrLookingFor = doc.DocumentNode.
SelectNodes("//div").
First().
SelectNodes("//div").
First().
InnerText;
Console.WriteLine(whatUrLookingFor);
Console.ReadKey(true);
我怎样才能让它工作?
【问题讨论】:
-
您到底尝试了什么?展示你最好的代码版本并解释你卡在哪里。
-
@ADyson ``` string stringThatKeepsYourHtml = @"
Title "; HtmlDocument 文档 = 新 HtmlDocument(); doc.LoadHtml(stringThatKeepsYourHtml); string whatUrLookingFor = doc.DocumentNode.SelectNodes("//div").First().SelectNodes("//div").First().InnerText; Console.WriteLine(whatUrLookingFor); Console.ReadLine();```你好 -
嗨。如您所见,将代码放在这样的 cmets 中时,很难阅读代码。 * 不像一个标准的讨论论坛(有关更多信息,请参阅tour)。您的问题下方有一个“编辑”按钮,您可以使用它来改进主要问题,然后信息可见,都在一个地方,并且可以正确格式化。为了让我们充分考虑您的代码,请编辑您的问题并将其放在那里。 How do I format my posts? 也可能对阅读有用。谢谢。
-
第二个
//div再次从根开始。以点开头:.//div -
@HansKesting thx
标签: c# html html-agility-pack