javascript - XPath 可以用来搜索 <script> block 吗?

标签 javascript php dom xpath selector

我擅长选择各种 HTML 内容。因此,我充满信心地创建了一些应该提取网站内容的代码,我偶然发现了一些奇怪的 JavaScript 代码,其中源代码将其价格放入其中。

<script>
 var productConfig = {"attributes":{"178":{"id":"178","code":"bp_flavour","label":"Smaak","options":[{"id":"28","label":"Aardbeien","oldPrice":"0","products":["2292","2294","2296","2702"]}

....更多的乱码,每个产品变体超过 4 个:(就像这样 80 个不同的行:)

,"childProducts":{
"2292":"price":"64.99","finalPrice":"64.99","no_of_servings":"166","178":"27","179":"34"},
"2292":"price":"17.99","finalPrice":"17.99","no_of_servings":"33","178":"28","179":"25"}
}


</script>

显然2292是手头产品的id。我想读出“finalPrice”。

我的 PHP 代码:

    $file = $this->curl_get_file_contents($url);
    $doc = new DOMDocument();
   @$doc->loadHTML($file);
    $doc->preserveWhiteSpace = false;
    $finder = new DomXPath($doc);

    $price_query = $finder->query("//script[contains(.,'finalPrice')]");
    $price_raw = $price_query->item(0)->nodeValue;

但是我的查询 //script[contains(.,"finalPrice")] 爆炸了整个脚本,我无法找到一种方法来更深入、更具体地挖掘 JavaScript。有谁知道更多/可以给我提示吗?

最佳答案

您可以尝试正则表达式:

preg_match_all("/finalPrice\\":\\"([0-9.]{1,10})\\"/", $page_html, $output_array);

关于javascript - XPath 可以用来搜索 &lt;script&gt; block 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31718783/

相关文章:

javascript - 这段代码中的 “export default class Render”是什么,为什么要导出和使用它?

javascript - 火存储 : can't match midnight with date filter in where() clause

php - 在附加的 HTML 上使用 jQuery 效果

javascript - 在您选择其中任何一个之前,将显示带有空白的 HTML 选择框

javascript - 全局关闭 Webpack 的延迟加载,没有魔法注释

javascript - 为什么 am 图表导出顶部的饼图/图例黑色?

php - 使用 RESTful API 调用注册用户

php - 统计mysql中的出现次数

javascript - 无法从 IE 8 中的列表框中获取选定的值

php - 如何从 DomDocument::saveHTML 中删除换行符