我知道有一个amazon API可以提取他们的数据,但我只是在尝试学习如何获取自己的知识,从amazon提取数据似乎是一个很好的测试。
<?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;
?>我所要做的就是从链接中拉出产品描述,但我不确定它为什么会起作用。我没有得到任何错误或任何数据,真的。
发布于 2014-08-06 06:54:28
Product Description的类是简单的productDescriptionWrapper,因此在示例代码中使用该css选择器
foreach($html->find('.productDescriptionWrapper') as $element) {
echo $element->plaintext . '<br />';
}simplehtmldom使用css选择器,与jQuery非常相似。因此,如果您希望所有的div都是->find('div'),如果您希望所有锚的类都是'hotProduct‘,那么就需要->find('a.hotProduct'),依此类推
发布于 2015-02-11 01:23:08
它不起作用,因为JavaScript正在将产品描述添加到iFrame中。
发布于 2018-10-01 13:44:31
您可以首先检查是否有从亚马逊获取的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);注意,你有http://,的https://,可能就是你什么都得不到的原因。一旦你得到了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已就位。您可以尝试这些xpath
https://stackoverflow.com/questions/25148450
复制相似问题