【问题标题】:Scraping data from amazon从亚马逊抓取数据
【发布时间】:2014-08-05 21:15:59
【问题描述】:

我知道有一个亚马逊 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;

?>

我想做的只是从链接中提取产品描述,但我不确定它为什么会起作用。真的,我没有收到任何错误或任何数据。

【问题讨论】:

  • 一方面,您的代码存在语法问题。带有您的 URL 的行不以撇号结尾来关闭字符串。
  • 如何选择一个没有明确违反他们条款的网站。
  • 抱歉,解决了 URL 的问题,但这是我复制和粘贴的问题,并且不在脚本中。

标签: php web-scraping


【解决方案1】:

产品描述的类只是productDescriptionWrapper,因此在您的示例代码中使用该css选择器

foreach($html->find('.productDescriptionWrapper') as $element) {
    echo $element->plaintext . '<br />';
}

simplehtmldom 使用与 jQuery 非常相似的 css 选择器。所以如果你想要所有的 div 说 -&gt;find('div') 如果你想要所有具有“hotProduct”类的锚说 -&gt;find('a.hotProduct') 等等

【讨论】:

    【解决方案2】:

    它不起作用,因为产品描述是通过 JavaScript 添加到 iFrame 中的。

    【讨论】:

      【解决方案3】:

      您首先可以检查是否有从亚马逊获取的 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-23
        • 2017-07-28
        • 2019-08-11
        • 1970-01-01
        • 2023-03-31
        • 2010-12-18
        • 1970-01-01
        相关资源
        最近更新 更多