我知道有一个亚马逊API用于提取他们的数据,但我只是想学习自己的知识并从亚马逊中提取数据似乎是一个很好的测试。
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
include('../includes/simple_html_dom.php');
$html = file_get_html('http://www.amazon.co.uk/gp/product/B00AZYBFGY/ref=s9_simh_gw_p86_d0_i1?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-2&pf_rd_r=1MP0FXRF8V70NWAN3ZWW&pf_r$')
foreach($html->find('a-section') as $element) {
echo $element->plaintext . '<br />';
}
echo $ret;
?>
我尝试做的只是从链接中提取产品说明,但我不确定它为什么会起作用。我真的没有收到任何错误或任何数据。
答案 0 :(得分:0)
产品描述的类只是productDescriptionWrapper
所以在您的示例代码中使用该css选择器
foreach($html->find('.productDescriptionWrapper') as $element) {
echo $element->plaintext . '<br />';
}
simplehtmldom使用与jQuery非常相似的css选择器。所以如果你想让所有的div都说->find('div')
,你想要所有的锚点都有一个&#39; hotProduct&#39;说->find('a.hotProduct')
等等等等
答案 1 :(得分:0)
它不起作用,因为JavaScript将产品描述添加到iFrame中。
答案 2 :(得分:0)
您首先可以检查是否有来自Amazon的HTML。它可能会阻止您的请求。
$url = "https://www.amazon.co.uk/gp/product/B00AZYBFGY/ref=s9_simh_gw_p86_d0_i1?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-2&pf_rd_r=1MP0FXRF8V70NWAN3ZWW&pf_r$"
$htmlContent = file_get_contents($url);
echo $htmlContent;
$html = str_get_html($htmlContent);
请注意,https://,您拥有http://,也许这就是为什么您什么都没得到的原因。 一旦获得HTML,就可以继续。 尝试其他选择器:
foreach($html->find('div[id=productDescription]')) as $element) {
echo $element->plaintext . '<br />';
}
foreach($html->find('div[id=content]')) as $element) {
echo $element->plaintext . '<br />';
}
foreach($html->find('div[id=feature-bullets]')) as $element) {
echo $element->plaintext . '<br />';
}
它应该显示页面本身,也许缺少一些CSS。 HTML是否到位。您可以尝试使用这些xpaths