前提・実現したいこと
特定の要素の配下にある画像パスを取得を試みてます。
Xpathが正しいのかわかりませんが、配列が空になります。
下記でおかしな点などはありますでしょうか。
回答よろしくお願いします。
該当のソースコード
<?php ```PHP $url = 'https://www.example.com/japanese'; $timeout = 60; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $result = curl_exec($ch); curl_close($ch); $dom = new DOMDocument(); $dom->loadHTML($result); $xpath = new DOMXPath($dom); $entries = $xpath->query('//div[@id="itemslider-new"]/div[@class="owl-wrapper-outer"]/div[@class="owl-wrapper"]/div[@class="owl-item"]'); $results = array(); foreach ($entries as $entry) { $result = array(); $result['img'] = $xpath->query("//img", $entry)->item(0)->getAttribute('src'); $results[] = $result; } ```HTML
1<div id="itemslider-new"> 2 <div class="owl-wrapper-outer"> 3 <div class="owl-wrapper"> 4 <div class="owl-item"> 5 <div class="item"> 6 <div class="product-image-wrapper" style="max-width:196px;"> 7 <a href="https://www.example.com/japanese/cramshell-cr02002"> 8 <img class="lazyOwl" src="https://www.example.com/media/catalog/product/cache/1/small_image/196x/040ec09b1e35df139433887a97daa66f/c/r/cr02002-1.jpg"> 9 </a> 10 </div> 11 </div> 12 </div> 13 <div class="owl-item"> 14 <div class="item"> 15 <div class="product-image-wrapper" style="max-width:196px;"> 16 <a href="https://www.example.com/japanese/cramshell-cr02002"> 17 <img class="lazyOwl" src="https://www.example.com/media/catalog/product/cache/1/small_image/196x/040ec09b1e35df139433887a97daa66f/c/r/cr02002-2.jpg"> 18 </a> 19 </div> 20 </div> 21 </div> 22 <div class="owl-item"> 23 <div class="item"> 24 <div class="product-image-wrapper" style="max-width:196px;"> 25 <a href="https://www.example.com/japanese/cramshell-cr02002"> 26 <img class="lazyOwl" src="https://www.example.com/media/catalog/product/cache/1/small_image/196x/040ec09b1e35df139433887a97daa66f/c/r/cr02002-3.jpg"> 27 </a> 28 </div> 29 </div> 30 </div> 31 <div class="owl-item"> 32 <div class="item"> 33 <div class="product-image-wrapper" style="max-width:196px;"> 34 <a href="https://www.example.com/japanese/cramshell-cr02002"> 35 <img class="lazyOwl" src="https://www.example.com/media/catalog/product/cache/1/small_image/196x/040ec09b1e35df139433887a97daa66f/c/r/cr02002-4.jpg"> 36 </a> 37 </div> 38 </div> 39 </div> 40 <div class="owl-item"> 41 <div class="item"> 42 <div class="product-image-wrapper" style="max-width:196px;"> 43 <a href="https://www.example.com/japanese/cramshell-cr02002"> 44 <img class="lazyOwl" src="https://www.example.com/media/catalog/product/cache/1/small_image/196x/040ec09b1e35df139433887a97daa66f/c/r/cr02002-5.jpg"> 45 </a> 46 </div> 47 </div> 48 </div> 49 </div> 50 </div> 51</div>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/01 16:06