简体   繁体   English

DOMXPath / PHP-如何从foreach中的parrent节点获取属性

[英]DOMXPath/PHP - How to get attribute from parrent node in foreach

Here is my code: 这是我的代码:

$url = "http://www.sportsdirect.com/flash-sale";
$statuss = 1;


$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);  



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

    $xpath = new DOMXpath($doc);

    $Sizes = $xpath->query('//ul[@class="bxslider"]//li//a/img');
    $AddSlides = $xpath->query('//ul[@class="bxslider2"]//li//a/img');
    $AddSlides2 = $xpath->query('//ul[@class="bxslider3"]//li//a/img');
    $N = 1;
    $i = 0;
    foreach ($Sizes as $item) {
        $N++;
        $i++;
        $ImageAlt = $item->getAttribute("alt");
        $Image = $item->getAttribute("src");
        $DataImage = $item->getAttribute("data-src");

        $Link = $xpath->query('//ul[@class="bxslider"]//li/a')->item(0)->getAttribute("href");

        if($DataImage == ""){
        echo "<img src='$Image'> LINK: $Link <br>";
        //$write->insert("responsivebannerslider_slide", array("group_names" => "1", "titles" => "$ImageAlt", "img_video" => "image", "img_hosting" => "1", "hosted_url" => "$Image", "url" => "$url", "url_target" => "new_window", "url_target" => "new_window", "date_enabled" => "0","sort_order" => "$N", "type" => "1", "statuss" => "$statuss"));
        }
        if($DataImage != ""){
            //$write->insert("responsivebannerslider_slide", array("group_names" => "1", "titles" => "$ImageAlt", "img_video" => "image", "img_hosting" => "1", "hosted_url" => "http://images.sportsdirect.com$DataImage", "url" => "$url", "url_target" => "new_window", "url_target" => "new_window", "date_enabled" => "0", "sort_order" => "$N",  "type" => "1", "statuss" => "$statuss"));
        echo "<img src='http://images.sportsdirect.com$DataImage'> LINK: $Link <br>";
        }

Here is the DOM HTML: 这是DOM HTML:

<ul class="bxslider">          
    <li><a href="/flash-sale-three">
    <img class="img-responsive" src="//images.sportsdirect.com/images/marketing/flash-sale-slide-160803.jpg" alt="Flash Sale">
    </a></li> 

    <li><a href="/pricecrash/price-crash-3">
    <img class="img-responsive" data-src="/images/marketing/Price-Crash-160803.jpg" alt="Price Crash">
    </a>
    </li> 

    <li><a href="/special-offer">
    <img class="img-responsive" data-src="/images/marketing/weekly-offer-front-site-160721.jpg" alt="Offer Of The Week">
    </a>
    </li> 
</ul>

I am able to get all the images but i want to get the links which the parrent <a href="TARGET-HERE"></a> holds. 我能够获取所有图像,但是我想获取父母<a href="TARGET-HERE"></a>持有的链接。

How i am suposed to get the parrent node attribute? 我如何获得父节点属性?

I get all images with this ul[@class="bxslider"]//li//a/img but i can not take only this ul[@class="bxslider"]//li/a in the foreach loop and take the attribute href why? 我使用此ul[@class="bxslider"]//li//a/img获得所有图像,但是我不能在foreach循环中仅接受此ul[@class="bxslider"]//li/a并接受属性href为什么?

I hope i have explained it well, if no please ask me what you do not understand. 我希望我已经解释清楚了,如果没有,请问我您不明白的地方。

Where is my mistake? 我的错误在哪里?

您可以采用$ item父节点的属性,因此:

$Link = $item->parentNode->getAttribute("href");

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

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