【发布时间】:2021-04-15 13:32:12
【问题描述】:
来自 API 的响应是包含完整 HTML 和 CSS 内容的网页。我唯一想要的是正文中的内容。
如何从网页中提取正文内容?
以下是网页的简短版本。页面很长,我不能在这里发布所有内容。
我要提取的正文内容是“嗨,John,Doe 祝你周年快乐,并希望 FCMB 的所有人也祝你生日快乐,恭喜你的周年纪念 Doe”
<!DOCTYPE html>
<html>
<head>
<style>
body {padding: 0; margin: 0; font-family: sans-serif;}
.general-container {min-height: 100vh; border-radius: 6px; }
</style>
</head>
<body>
<div class="modal fade" id="CustomerPreviewMsg" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-content">
<div class="modal-body mb-0 p-0">
<div class="row mx-0 col-12 profile-pic-container">
<p class="pt-3">
Hi John, Doe wishes you a happy anniversary and wants all of us at FCMB to wish you same, Congratulations on your anniversary Doe
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script src="/Scripts/jquery-3.3.1.js"></script>
<script src="/JsFile/MainJs.js"></script>
<script src="https://unpkg.com/wavesurfer.js"></script>
这是使用端点的代码
var client = new RestClient(appSettings.ShoutOutPreviewUrl + previewMessage.MessageHistoryId);
client.AddDefaultHeader("Authorization", string.Format("Bearer {0}", appSettings.ShoutOutToken));
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "text/plain");
IRestResponse response = await client.ExecuteAsync(request);
IRestResponse<string> res = client.Execute<string>(request);
return res.Content;
【问题讨论】:
-
这是静态的html页面还是动态的?如果是动态的,您是否使用剃刀渲染视图?
-
只有正文内容是动态的。 html是静态的
-
那么你是用剃须刀引擎渲染的吗?原始视图是什么样的?我再次假设您使用的是 MVC 或 Razor 视图页面。
-
当我在浏览器中预览页面时,它就像一个模式,我没有创建 API。我只是在使用 API 从我的 asp.net 核心获取动态内容。我应该在 reactJs 模式中显示内容。我认为最好将内容提取并显示在前端模式中
-
使用 XML 解析器。如果您要提取的节点的路径在对端点的调用之间是静态的,那么您应该能够使用 XPath 查询轻松地将其拉出。
标签: c# html asp.net-core restsharp