모든 종류의 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에서 더 깊고 구체적으로 파헤치는 방법을 찾을 수없는 전체 스크립트를 폭파합니다. 누구든지 더 알고 / 나에게 힌트를 줄 수 있습니까?
그래서 내가 한 일은 제공된 XPATH 쿼리로 스크립트를 읽습니다. Than : 내가 원하는 json 부분을 얻을 때까지 strstr. 다음은 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 ftw!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다