如何在邮递员网站的HTML中提取产品名称
我正在尝试验证产品是否已添加到移动网络视图结帐中。我尝试过Cheerios但没能从中得到太多好处。
在 HTML 响应中,我可以看到下面的脚本,其中包含产品信息,我想从电子商务数组内的该脚本中获取产品的名称 Short Sleeve Polo
。提前致谢
<script>
pageContext = {
"currentPage":"shipping",
"title":"Checkout",
"type":"checkout",
"ns":"checkout",
"analytics":{
"user":{
"customerType":"New",
"ecomStore":"Demandware",
"userType":"regular user",
"gender":"Female",
"hasTransacted":false,
"userEmailAddress":"[email protected]",
"userId":"W05dds015457",
"socialNetwork":""
},
"basket":{
"ecommerce":[
{
"name":"Short Sleeve Polo Shirt",
"id":"AA1s5083",
"variationid":"AA15s083001",
"isEdits":null,
"price":"10.00",
"brand":"Test",
"category":"variation-masters",
"variant":"Fresh Blue|3/4",
"quantity":1,
"position":0
},
{
"name":"Limited Edition Short Sleeve Polo Shirt",
"id":"AA15080",
"variationid":"AA15080007",
"isEdits":null,
"price":"12.00",
"brand":"Test",
"category":"kidswear",
"variant":"Dusky Jade|3/4",
"quantity":1,
"position":1
}
]
},
"page":{
"currencyCode":"GBP",
"pageCategory":"shipping",
"subCategory":"shipping",
"pageName":"SiteGenesis Checkout",
"pageDesc":"",
"pageLocale":"en_GB",
"pageCurrency":"GBP",
"pageType":"shipping",
"user":"regular user"
}
},
"billingAllowed":false
};
</script>
I am trying to verify if products has been added in mobile webview checkout. I tried Cheerios but not been able to get much out of it.
In the HTML response I can see below script which has products information and I would like to get the name Short Sleeve Polo
of the product from this script inside ecommerce array. Thanks in advance
<script>
pageContext = {
"currentPage":"shipping",
"title":"Checkout",
"type":"checkout",
"ns":"checkout",
"analytics":{
"user":{
"customerType":"New",
"ecomStore":"Demandware",
"userType":"regular user",
"gender":"Female",
"hasTransacted":false,
"userEmailAddress":"[email protected]",
"userId":"W05dds015457",
"socialNetwork":""
},
"basket":{
"ecommerce":[
{
"name":"Short Sleeve Polo Shirt",
"id":"AA1s5083",
"variationid":"AA15s083001",
"isEdits":null,
"price":"10.00",
"brand":"Test",
"category":"variation-masters",
"variant":"Fresh Blue|3/4",
"quantity":1,
"position":0
},
{
"name":"Limited Edition Short Sleeve Polo Shirt",
"id":"AA15080",
"variationid":"AA15080007",
"isEdits":null,
"price":"12.00",
"brand":"Test",
"category":"kidswear",
"variant":"Dusky Jade|3/4",
"quantity":1,
"position":1
}
]
},
"page":{
"currencyCode":"GBP",
"pageCategory":"shipping",
"subCategory":"shipping",
"pageName":"SiteGenesis Checkout",
"pageDesc":"",
"pageLocale":"en_GB",
"pageCurrency":"GBP",
"pageType":"shipping",
"user":"regular user"
}
},
"billingAllowed":false
};
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是获取脚本文本,定位变量的 JSON,解析它,然后最终像普通数组/对象一样定位您想要的值。
参考: 使用 jQuery/cheerio 访问脚本标记中的变量
One way you can do it is by getting the text of the script, targeting the JSON of the variable, parsing it, then finally targetting the values you want just like a normal array/object.
Ref: Accessing variable in script tag with jQuery/cheerio