如何在邮递员网站的HTML中提取产品名称

发布于 2025-01-09 01:03:29 字数 2194 浏览 1 评论 0原文

我正在尝试验证产品是否已添加到移动网络视图结帐中。我尝试过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2025-01-16 01:03:29

一种方法是获取脚本文本,定位变量的 JSON,解析它,然后最终像普通数组/对象一样定位您想要的值。

const cheerio = require('cheerio');
const $ = cheerio.load(html); // your html

const text = $('script')[0].text(); // TODO there might be multiple script tags

// get variable pageContext in the text
const matches = text.match(/pageContext = ([^;]*);/);

// removes any possible '\n' that show up and assign
// matches[1] (which now contains the json text) to a variable
var json = matches[1].replace(/\n/gi, '');

// parse the JSON that has been collected
var pageContext = JSON.parse(json);

// get both names from the ecommerce array items
var value1 = pageContext.analytics.basket.ecommerce[0].name;
var value2 = pageContext.analytics.basket.ecommerce[1].name;

参考: 使用 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.

const cheerio = require('cheerio');
const $ = cheerio.load(html); // your html

const text = $('script')[0].text(); // TODO there might be multiple script tags

// get variable pageContext in the text
const matches = text.match(/pageContext = ([^;]*);/);

// removes any possible '\n' that show up and assign
// matches[1] (which now contains the json text) to a variable
var json = matches[1].replace(/\n/gi, '');

// parse the JSON that has been collected
var pageContext = JSON.parse(json);

// get both names from the ecommerce array items
var value1 = pageContext.analytics.basket.ecommerce[0].name;
var value2 = pageContext.analytics.basket.ecommerce[1].name;

Ref: Accessing variable in script tag with jQuery/cheerio

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文