XPath 可以用于搜索 <script> 块吗?
Can XPath be used to search a <script> block?
我在选择各种 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 中找到更深入和更具体的挖掘方法。有谁知道more/could给我提示吗?
您可以试试正则表达式:
preg_match_all("/finalPrice\":\"([0-9.]{1,10})\"/", $page_html, $output_array);
您可以像这样从对象中读取属性。
var obj = {"2292":{"price":"64.99","finalPrice":"64.99","no_of_servings":"166","178":"27","179":"34"}};
obj['2292']['finalPrice']
所以我做了什么:用提供的 XPATH 查询读出脚本。比:strstr 直到我得到我想要的 json 部分。接下来是:PHP 的 json_decode 函数。将它放在一个数组中,而不是在数组中搜索我想要的内容。这是我的解析代码:
$price_query = $finder->query("//script[contains(.,'finalPrice')]");
$price_raw = $price_query->item(0)->nodeValue;
$price_1 = strstr($price_raw, "childProducts");
$price_2 = str_replace('childProducts":', '', $price_1);
$price_3 = strstr($price_2, ',"priceFromLabel"', true);
$price_data = json_decode($price_3, true);
str str 看起来像废话,但有效。谢谢大家的想法。 json_decode好吧!
我在选择各种 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 中找到更深入和更具体的挖掘方法。有谁知道more/could给我提示吗?
您可以试试正则表达式:
preg_match_all("/finalPrice\":\"([0-9.]{1,10})\"/", $page_html, $output_array);
您可以像这样从对象中读取属性。
var obj = {"2292":{"price":"64.99","finalPrice":"64.99","no_of_servings":"166","178":"27","179":"34"}};
obj['2292']['finalPrice']
所以我做了什么:用提供的 XPATH 查询读出脚本。比:strstr 直到我得到我想要的 json 部分。接下来是:PHP 的 json_decode 函数。将它放在一个数组中,而不是在数组中搜索我想要的内容。这是我的解析代码:
$price_query = $finder->query("//script[contains(.,'finalPrice')]");
$price_raw = $price_query->item(0)->nodeValue;
$price_1 = strstr($price_raw, "childProducts");
$price_2 = str_replace('childProducts":', '', $price_1);
$price_3 = strstr($price_2, ',"priceFromLabel"', true);
$price_data = json_decode($price_3, true);
str str 看起来像废话,但有效。谢谢大家的想法。 json_decode好吧!