亚马逊产品的PHP XPath问题


PHP XPath issue for amazon products

我想从下面的URL和使用下面的Xpath查询获得所有图像,但每次查询都返回null。

网址:

http://www.amazon.com/gp/browse.html?ie=UTF8&marketplaceID=ATVPDKIKX0DER&me=A219HML0CVO0HP  

Xpath查询:

$products = $xpath->evaluate('//div[@class="productTitle"]//img');  

我相信你在img之前有太多的前斜杠:

$xpath->evaluate('//div[@class="productTitle"]/img');

这应该与该链接中的以下HTML相匹配:

<div id="srProductTitle_B0000CBIFG_0" class="productTitle">
    <a href="https://rads.stackoverflow.com/amzn/click/com/B0000CBIFG" rel="nofollow noreferrer">
    <img src="http://ecx.images-amazon.com/images/I/51BZs4Gf5pL._SL160_AA160_.jpg" class="" border="0" alt="Product Details"  width="160" height="160"/><br clear="all" />Weed Eater 952701594 0.065-Inch-by-200-Foot Bulk Round String Trimmer Line
    </a>
</div>

这可能会帮助你。。。

$subject = file_get_contents('http://www.amazon.com/gp/browse.html?ie=UTF8&marketplaceID=ATVPDKIKX0DER&me=A219HML0CVO0HP');
$string = preg_replace('/'s's+/', '', $subject);
preg_match_all('/<a(.*?)href="(.*?)">(.*?)<img(.*?)src="(.*?)"(.*?)class=""(.*?)border="0"(.*?)alt="Product(.*?)Details/', $subject, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
    echo "<pre>";
    echo $result[5][$i];
}

谢谢。。。。。p2c