DOMXPath / cURL - 尝试将多个元素放入foreach循环中

时间:2016-06-30 13:44:52

标签: php html dom curl

我正在从此页面解析DOM元素:http://www.sportsdirect.com/kappa-nulent-4-trainers-child-boys-039063?colcode=03906301

以下是我现在关注的特定DOM:

<ul id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_ulSizes" class="sizeButtons" data-clicktoselecttext="Click to select">

    <li id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl00_liItem" role="radio" title="Click to select C10 (28)" data-text="C10 (28)" class="tooltip sizeButtonli ">
        <a href="DesktopModules/SportsDirect/ProductDetail/Controls/#" id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl00_ancLink">C10 (28)</a>
    </li>

    <li id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl01_liItem" role="radio" title="Click to select C11 (29)" data-text="C11 (29)" class="tooltip sizeButtonli ">
        <a href="DesktopModules/SportsDirect/ProductDetail/Controls/#" id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl01_ancLink">C11 (29)</a>
    </li>

    <li id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl02_liItem" role="radio" title="Click to select C12 (31)" data-text="C12 (31)" class="tooltip sizeButtonli ">
        <a href="DesktopModules/SportsDirect/ProductDetail/Controls/#" id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl02_ancLink">C12 (31)</a>
    </li>

    <li id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl03_liItem" role="radio" title="Click to select C13 (32)" data-text="C13 (32)" class="tooltip sizeButtonli ">
        <a href="DesktopModules/SportsDirect/ProductDetail/Controls/#" id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl03_ancLink">C13 (32)</a>
    </li>

    <li id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl04_liItem" role="radio" title="Click to select 1 (33)" data-text="1 (33)" class="tooltip sizeButtonli ">
        <a href="DesktopModules/SportsDirect/ProductDetail/Controls/#" id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl04_ancLink">1 (33)</a>
    </li>

    <li id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl05_liItem" role="radio" title="Click to select 2 (34)" data-text="2 (34)" class="tooltip sizeButtonli ">
        <a href="DesktopModules/SportsDirect/ProductDetail/Controls/#" id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_rptSizes_ctl05_ancLink">2 (34)</a>
    </li>

</ul>

这是我的代码:

<?PHP 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_COOKIE, "ChosenSite=www; SportsDirect_AnonymousUserCurrency=GBP; language=en-GB");
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSLVERSION, 3);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    $str = curl_exec($curl);  
    curl_close($curl);  

    $html= str_get_html($str);

    libxml_use_internal_errors(true); 
    $doc = new DOMDocument();
    $doc->loadHTML($str);

    $xpath = new DOMXpath($doc);


    $i = 0;
    $Sizes = $xpath->query('//ul[@id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_ulSizes"]//li/a');
    foreach ($Sizes->item() as $item) {
        $title = $item->nodeValue;
        $N = $i++;  
?>
        <div class="SizeButtons <?PHP if($N ==  0){ echo "SizeSelected";} ?>" data-title="<?PHP echo $title;?>" data-number="<?PHP echo $N;?>"><?PHP echo $title; ?></div>    
    }         
?>

不幸的是,似乎我的代码错误,因为我可以得到结果。正如您所看到的,//ul[@id="dnn_ctr103511_ViewTemplate_ctl00_ctl14_ulSizes"]//li/a下有几个DOM元素,但我无法得到它们。

我的错误在哪里,我该如何解决?

提前致谢!

1 个答案:

答案 0 :(得分:0)

我发现了我的错误!

foreach ($Sizes->item() as $item) {行中,只需删除->item()愚蠢的我..

感谢大家的帮助!