我想从以下 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