繁体   English   中英

使用Java从HTML页面提取JavaScript字符串

[英]Extract JavaScript String from HTML page with Java

我想获取在html页面中硬编码的特定Javascript变量的值。 按照以下说明访问测试用例:

  • 转到网站: http : //www.headphonezone.in/
  • 打开控制台
  • 类型: Shopify.theme
  • 输出为: Object {name: "Retina", id: 8528293, theme_store_id: 601, role: "main"}
  • 类型: Shopify.theme.theme_store_id
  • 输出为: 601

以上响应来自下面给出的脚本,该脚本存在于所有Shopify商店中。

<script>
//<![CDATA[
      var Shopify = Shopify || {};
      Shopify.shop = "headphone-zone.myshopify.com";
      Shopify.theme = {"name":"Retina","id":8528293,"theme_store_id":601,"role":"main"};

//]]>
</script>

如何编写Java代码以获取Shopify.theme.theme_store_id字段的值并将其存储?

  • 以字符串形式获取html页面(请参阅此文章
  • 使用正则表达式检测“ Shopify.theme”关键字:

String patternString = "Shopify.theme\\s*=\\s*.*theme_store_id\\"\\:(\\d+)";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);

String themeStoreId;
while (matcher.find()) {
    themeStoreId = matcher.group(1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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