[英]Postman: Extract data from javascript and HTML response into a JSON object
我正在使用 Postman 和 Cheerio 從 HTML 響應中抓取數據。 在下面的示例中,我試圖將DDC.dataLayer['b']
(在<script>
標簽中)中的數據拉入 JSON object。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
...
</head>
<body>
...
<script type="text/javascript">
(function(f1){
...
};
DDC.dataLayer['a'] = {
...
};
DDC.dataLayer['b'] = [{
"b1": "b1",
"b2": "b2",
"b3": {"b31":"01","b32":"name"}
}];
DDC.dataLayer.c = {
aa: [],
bb: [],
cc: []
};
</script>
</body>
</html>
我設法將腳本標簽之間的所有文本拉到一個變量中,但是,當我嘗試在其上使用 JSON.parse 時,由於 function f1 出現錯誤。 我如何將DDC.dataLayer['b']
拉到 Postman 中的 JSON object 中?
謝謝!
我創建了一個 API 顯示提取 JSON 的代碼:
API: https://wirespec.dev/Wirespec/projects/apis/Stackoverflow/apis/extractJSONFromHTML
響應: https://api.wirespec.dev/wirespec/stackoverflow/extractjsonfromhtml
Javascript Wirespec 使用的掛鈎: https://gist.github.com/Wirespec/fa30367192716d4bdece29
html.match(/DDC.dataLayer\['b'\] = (\[.*?\])/s)[1]
這? 讓它不貪婪
// find the start of the varaible you want
start = a.indexOf("DDC.dataLayer['b'] =")+"DDC.dataLayer['b'] =".length;
//find the end using ';'
stop = a.indexOf(";",a.indexOf("DDC.dataLayer['b']"))
console.log(start,stop)
// find the substring and parse it
var res = a.substring(start,stop)
console.log(JSON.parse(res))
將此添加到您的測試腳本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.