简体   繁体   中英

How to get a JavaScript variable value from HTML using AngleSharp?

I have a website url, that returns some html like below, and I want to get value of Trade variable. I am using AngleSharp library in ASP.NET Core v2.0

<div> <p>html content</p> </div>
<script type="text/javascript">
 var Trade=[['20190717','145'],['1455','5552']];
</script>

For such simple cases AngleSharp.Js should be able to help you.

Install it via NuGet Install-Package AngleSharp.Js .

Your configuration should be similar to (excluding any options you've used already - keep those in there):

var configuration = Configuration.Default.WithJs();

Now you could access some global JS variable in an existing document (instance of an IDocument ) from a valid context (instance of an IBrowsingContext ).

var engine = context.GetService<JsScriptingService>();
var jsValue = engine.EvaluateScript(document, "Trade");

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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