使用HTMLAgilityPack解析javascript HTML

c# html html-agility-pack parsing

我有以下HTML,我正在嘗試使用HTML Agility Pack進行解析。

這是一段HTML代碼:

<body id="station_page" class="">
...
<div>....</div>
<script type="text/javascript"> 
if (Blablabla == undefined) { var Blablabla = {}; }
Blablabla .Data1= "I want this data";
Blablabla .BlablablaData = 
{  "Data2":"I want this data",
"Blablabla":"",
"Blablabla":0   }
{   "Blablabla":123,
"Data3":"I want this data",
"Blablabla":123}
    Blablabla .Data4= I want this data;
</script>...

我想要獲得這4個數據變量(Data1,Data2,Data3,Data4)。首先,我試圖找到javascript:

<body id="station_page" class="">
...
<div>....</div>
<script type="text/javascript"> 
if (Blablabla == undefined) { var Blablabla = {}; }
Blablabla .Data1= "I want this data";
Blablabla .BlablablaData = 
{  "Data2":"I want this data",
"Blablabla":"",
"Blablabla":0   }
{   "Blablabla":123,
"Data3":"I want this data",
"Blablabla":123}
    Blablabla .Data4= I want this data;
</script>...

如何檢查它是否真的是正確的JavaScript?找到相關的javascript後,我怎樣才能得到那4個數據變量(Data1,Data2,Data3,Data4)?

熱門答案

您無法使用HTML Agility Pack解析javascript,它只支持HTML解析。你可以使用XPATH來獲得你需要的腳本:

doc.DocumentNode.SelectSingleNode("//script[contains(text(), 'Blablabla')]").InnerHtml

但是你需要用另一種方​​法解析javascript(正則表達式,js語法等)




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因