我正在PHP上使用Simple HTML DOM库,您可以在这里找到该库: Simple HTML DOM
我也在用Javascript解析HTML,主要是为了隔离我需要的选择器。
我首先尝试使用Javascript(使用querySelectorAll)进行解析,然后在工作时我将相同的选择器与PHP Simple HTML DOM(使用-> find)一起重新使用。
让我们用以下页面来说明问题:Page from Amazon.fr
$html = file_get_html('PAGE URL FROM EXAMPLE ABOVE');
$products = $html->find('.s-item-container');
for ($z = 0 ; $z < sizeof($products); $z++)
{
foreach($products[$z]->find('.a-row .a-spacing-none .s-access-detail-page') as $titles)
{
$title = $titles->plaintext;
}
}
这工作得很好,如果我回显$ title,我确实得到了正确的标题。
现在,如果我想用这个来获取价格:
$products[$z]->find('div > div.a-fixed-left-grid > div > div.a-fixed-left-grid-col.a-col-right > div:nth-child(2) > div.a-column.a-span7 > div.a-row.a-spacing-none > a > span.a-size-base.a-color-price.s-price.a-text-bold');
如果显示$ product [$ z]的内容,则可以看到我要查找的选择器,该选择器在html中可见,但是find函数找不到它。
我在Javascript中尝试了完全相同的操作:
document.querySelectorAll('div > div.a-fixed-left-grid > div > div.a-fixed-left-grid-col.a-col-right > div:nth-child(2) > div.a-column.a-span7 > div.a-row.a-spacing-none > a > span.a-size-base.a-color-price.s-price.a-text-bold');
通过返回以下内容可以正常工作:
我不明白为什么会发生这种情况,因为当它在JS中工作时也可以在PHP中工作,这是唯一一个不起作用并且我看不出问题出在哪里的例子。
你有什么主意吗?
谢谢! 洛朗(Laurent)
根据下面的请求,这里有一个转储:(很抱歉,它很难阅读)
<div class="s-item-container"><div class="a-fixed-left-grid"><div class="a-fixed-left-grid-inner" style="padding-left:218px"><div class="a-fixed-left-grid-col a-col-left" style="width:218px;margin-left:-218px;float:left;"><div class="a-row"><div aria-hidden="true" class="a-column a-span12 a-text-center"><a class="a-link-normal a-text-normal" href="https://www.amazon.fr/MSI-GEFORCE-GTX-1060-3GT/dp/B01KHWOB78/ref=sr_1_18?ie=UTF8&qid=1533908456&sr=8-18&keywords=GTX+1060"><img src="https://images-eu.ssl-images-amazon.com/images/I/518zhXqNWcL._AC_US218_.jpg" srcset="https://images-eu.ssl-images-amazon.com/images/I/518zhXqNWcL._AC_US218_.jpg 1x, https://images-eu.ssl-images-amazon.com/images/I/518zhXqNWcL._AC_US327_FMwebp_QL65_.jpg 1.5x, https://images-eu.ssl-images-amazon.com/images/I/518zhXqNWcL._AC_US436_FMwebp_QL65_.jpg 2x, https://images-eu.ssl-images-amazon.com/images/I/518zhXqNWcL._AC_US500_FMwebp_QL65_.jpg 2.2935x" width="218" height="218" alt="MSI GEFORCE GTX 1060 3GT OC Carte Graphique , 3 GB" class="s-access-image cfMarker" data-search-image-load></a><div class="a-section a-spacing-none a-text-center"></div></div></div></div><div class="a-fixed-left-grid-col a-col-right" style="padding-left:2%;float:left;"><div class="a-row a-spacing-small"><div class="a-row a-spacing-none"><a class="a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal" title="MSI GEFORCE GTX 1060 3GT OC Carte Graphique , 3 GB" href="https://www.amazon.fr/MSI-GEFORCE-GTX-1060-3GT/dp/B01KHWOB78/ref=sr_1_18?ie=UTF8&qid=1533908456&sr=8-18&keywords=GTX+1060"><h2 data-attribute="MSI GEFORCE GTX 1060 3GT OC Carte Graphique , 3 GB" data-max-rows="0" class="a-size-medium s-inline s-access-title a-text-normal">MSI GEFORCE GTX 1060 3GT OC Carte Graphique , 3 GB</h2></a></div><div class="a-row a-spacing-none"><span class="a-size-small a-color-secondary">de </span><span class="a-size-small a-color-secondary">MSI</span></div></div><div class="a-row"><div class="a-column a-span7"><div class="a-row a-spacing-none"><a class="a-link-normal a-text-normal" href="https://www.amazon.fr/MSI-GEFORCE-GTX-1060-3GT/dp/B01KHWOB78/ref=sr_1_18?ie=UTF8&qid=1533908456&sr=8-18&keywords=GTX+1060"><span class="a-size-small a-color-secondary"></span><span class="a-size-base a-color-price s-price a-text-bold">EUR 237,36</span></a><span class="a-letter-space"></span><span class="a-size-small a-color-secondary">+ EUR 14,90 Livraison</span></div><div class="a-row a-spacing-mini"><div class="a-row a-spacing-none"><span class="a-size-small a-color-price">Plus que 4 ex. Commandez vite !</span></div></div><div class="a-row a-spacing-mini"><div class="a-row a-spacing-none"><div class="a-row a-spacing-mini"></div><span class="a-size-small a-color-secondary">Autres vendeurs sur Amazon</span></div><div class="a-row a-spacing-none"><a class="a-size-small a-link-normal a-text-normal" href="https://www.amazon.fr/gp/offer-listing/B01KHWOB78/ref=sr_1_18_olp?ie=UTF8&qid=1533908456&sr=8-18&keywords=GTX+1060"><span class="a-color-secondary a-text-strike"></span><span class="a-size-base a-color-price a-text-bold">EUR 176,54</span><span class="a-letter-space"></span>(28 d’occasion & neufs)</a></div></div></div><div class="a-column a-span5 a-span-last"><div class="a-row a-spacing-mini"><span name="B01KHWOB78"> <span class="a-declarative" data-action="a-popover" data-a-popover="{"max-width":"700","closeButton":"false","position":"triggerBottom","url":"/review/widgets/average-customer-review/popover/ref=acr_search__popover?ie=UTF8&asin=B01KHWOB78&contextId=search&ref=acr_search__popover"}"><a href="javascript:void(0)" class="a-popover-trigger a-declarative"><i class="a-icon a-icon-star a-star-4"><span class="a-icon-alt">4,2 étoiles sur 5</span></i><i class="a-icon a-icon-popover"></i></a></span></span> <a class="a-size-small a-link-normal a-text-normal" href="https://www.amazon.fr/MSI-GEFORCE-GTX-1060-3GT/dp/B01KHWOB78/ref=sr_1_18?ie=UTF8&qid=1533908456&sr=8-18&keywords=GTX+1060#customerReviews">18</a></div><div class="a-row a-spacing-mini"><span class="a-size-small a-color-secondary a-text-bold">Descriptions du produit</span><br><span class="a-size-small a-color-secondary">... La carte graphique MSI GeForce <em>GTX 1060</em> 3GT OC met la VR (Réalit ...</span></div></div></div></div></div></div></div>