繁体   English   中英

如何获取在 ASP.NET 核心中的 API 中返回的 web 页面的正文内容

[英]How to get the body content of a web page returned in an API in ASP.NET Core

API 的响应是 web 页面,其中包含完整的 HTML 和 Z2C56C360580420D293172F42D8 内容。 我唯一想要的是正文中的内容。

如何从 web 页面中提取正文内容?

以下是 web 页面的简短版本。 页面很长,我不能在这里发布所有内容。

我要提取的正文内容是“嗨,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">&times;</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;

经过一番挖掘,我使用 HtmlAgilityPack 获取节点https://html-agility-pack.net/我通过 nuget 安装

internal string ParseHtml(string Html)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(Html);

            var htmlNodes = doc.DocumentNode.SelectSingleNode("//p[@class='pt-3']");

            string rawText = htmlNodes.InnerText.Trim();

            return rawText;
        }

暂无
暂无

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

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