简体   繁体   English

PHP DOM解析器中断页面并且无法加载页面内容

[英]PHP DOM parser breaks the page and can't load page content

I have created a php parser that must extract the price in a span tag, but when I echo the $html so that I could see how the page loads, it shows me a broken page with no contents. 我创建了一个PHP分析器,该分析器必须在span标签中提取价格,但是当我回显$ html以便看到页面的加载方式时,它显示了一个没有内容的损坏页面。 Instead only header and footer loads, but not the content. 而是仅加载页眉和页脚,而不加载内容。 The content seems to load by JavaScript externally and my question is how can I load the html page with Dom so that JavaScript also loads? 内容似乎是通过JavaScript从外部加载的,我的问题是如何使用Dom加载html页面,以便也加载JavaScript? I need to let the whole content load so that I can get the divs and spans. 我需要让整个内容加载,以便获得div和span。 This is my code: 这是我的代码:

<?php

require_once('simple_html_dom.php');

$url = 'http://oldnavy.gap.com/browse/product.do?cid=99570&vid=1&pid=714649002';

$dom = new domDocument('1.0', 'UTF-8');
$html = file_get_html($url);

echo $html;

if(is_object($html)){

    foreach ( $html->find('span#priceText') as $data){

        $raw_price = $data->innertext;

        echo $raw_price;


    }
 }
?>

Alt aproach 替代方法

The link you are actually looking for (in his minimal expression) is this: http://oldnavy.gap.com/browse/productData.do?pid=714649 您实际上正在寻找的链接(以他的最小表达方式)是: http : //oldnavy.gap.com/browse/productData.do?pid=714649

Now load that using curl, put a value to the unknownShopperId cookie, explode it into an array and get the price you need: 现在使用curl加载它,将一个值添加到unknownShopperId cookie中,将其分解为一个数组并获得所需的价格:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "http://oldnavy.gap.com/browse/productData.do?pid=714649");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: unknownShopperId=E853DA3B2607DDAA5F2FE13CE8D32ACF"));

$result = curl_exec($ch);
$explode = explode(',', $result);

echo 'Original price: ' . $explode[92] . '<br/>' .
'New price: ' . $explode[93] . '<br/>' .
'Both prices: ' . $explode[13];

The result will be: '$14.94' 结果将是: '$14.94'

From now on, if you need another price you must know the intem's pid 从现在开始,如果您需要另一个价格,您必须知道intem的pid

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

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