Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP HTML DOM解析器Amazon提供列出所有价格和卖家名称_Php_Parsing_Dom_Html Parsing - Fatal编程技术网

PHP HTML DOM解析器Amazon提供列出所有价格和卖家名称

PHP HTML DOM解析器Amazon提供列出所有价格和卖家名称,php,parsing,dom,html-parsing,Php,Parsing,Dom,Html Parsing,我正试图从以下亚马逊报价列表页面中获取价格和卖家: 我可以通过以下方式获得价格: $ret['Retail'] = $html->find('span[class="price"]', 0)->innertext; 这是出价清单中的第一个价格 我试图通过使用以下命令从包含卖家名称的img中获取alt值,来拉取第一个价格的匹配卖家: $ret['SoldBy'] = $html->find('ul.sellerInformation img', 0)->getAttri

我正试图从以下亚马逊报价列表页面中获取价格和卖家:

我可以通过以下方式获得价格:

$ret['Retail'] = $html->find('span[class="price"]', 0)->innertext;
这是出价清单中的第一个价格

我试图通过使用以下命令从包含卖家名称的img中获取alt值,来拉取第一个价格的匹配卖家:

$ret['SoldBy'] = $html->find('ul.sellerInformation img', 0)->getAttribute('alt');
第一次成功了,但当我下楼时,它开始失去卖家,甚至在某些情况下失去了价格

谁能告诉我为什么它会错过卖家,甚至在价格上跳来跳去?我为获得更多卖家所做的一切是:

$ret['Retail2'] = $html->find('span[class="price"]', 1)->innertext;
$ret['SoldBy2'] = $html->find('ul.sellerInformation img', 1)->getAttribute('alt');
$ret['Retail3'] = $html->find('span[class="price"]', 2)->innertext;
$ret['SoldBy3'] = $html->find('ul.sellerInformation img', 2)->getAttribute('alt');
$ret['Retail4'] = $html->find('span[class="price"]', 3)->innertext;
$ret['SoldBy4'] = $html->find('ul.sellerInformation img', 3)->getAttribute('alt');
$ret['Retail5'] = $html->find('span[class="price"]', 4)->innertext;
$ret['SoldBy5'] = $html->find('ul.sellerInformation img', 4)->getAttribute('alt');
$ret['Retail6'] = $html->find('span[class="price"]', 5)->innertext;
$ret['SoldBy6'] = $html->find('ul.sellerInformation img', 5)->getAttribute('alt');
$ret['Retail7'] = $html->find('span[class="price"]', 6)->innertext;
$ret['SoldBy7'] = $html->find('ul.sellerInformation img', 6)->getAttribute('alt');

谢谢你的建议

我使用foreach并将结果放入数组中。由于卖家数量因商品而异,所以效果更好

<?php

$url = 'http://www.amazon.com/gp/offer-listing/B0036RNK7O/ref=dp_olp_new?ie=UTF8&qid=1319582305&sr=8-2';

$dom = new DomDocument();

$content = file_get_contents($url);
$dom->loadHTML($content);

$results = array();
$classes_to_collect = array('price', 'shipping_block', 'condition', 'sellerInformation');
$seller_elements = array('name', 'rating', 'stock_info', 'item_info');

foreach($dom->getElementsByTagName('tbody') as $tb)
{
  if($tb->hasAttribute('class') && stripos($tb->getAttribute('class'), 'result')!==false)
  {
    foreach($tb->getElementsByTagName('tr') as $tr)
    {
      $new_result = array();
      foreach($tr->getElementsByTagName('td') as $td)
      {
        foreach($td->childNodes as $cne)
        {
          foreach($classes_to_collect as $ctc)
          {
            if($cne->hasAttributes() && $cne->getAttribute('class') && stripos($cne->getAttribute('class'), $ctc)!==false)
            {
              if($cne->localName=='ul')
              {
                $new_sellern = array();
                $lis = $cne->getElementsByTagName('li');
                foreach($lis as $lii=>$lie)
                {
                  $value = $lie->textContent;
                  if($seller_elements[$lii]=='item_info')
                  {
                    $cutoff = strpos($value, 'amznJQ.onReady');
                    if($cutoff) $value = substr($value, 0, $cutoff);
                  }
                  else if($seller_elements[$lii]=='name')
                  {
                    $cutoff = strpos($value, 'Seller:');
                    if($cutoff!==false) $value = substr($value, 7);
                  }
                  else if($seller_elements[$lii]=='rating')
                  {
                    $cutoff = strpos($value, 'Seller Rating:');
                    if($cutoff!==false) $value = substr($value, 14);
                  }
                  $new_seller[$seller_elements[$lii]] = trim($value);
                }
                $new_result[$ctc] = $new_seller;
              }
              else $new_result[$ctc] = $cne->textContent;
            }
          }
        }
      }
      $results[] = $new_result;
    }
  }
}

print_r($results);
foreach($html->find('div.resultsset table tbody.result tr') as $article) {  
if($article->find('span.price', 0)) {   
// get retail   
$item['Retail'] = $article->find('span.price', 0)->plaintext;   
// get soldby   
if($article->find('img', 0)->getAttribute('alt') <> '') { 
$item['SoldBy'] = $article->find('img', 0)->getAttribute('alt'); } 
else {$item['SoldBy'] = $article->find('ul.sellerInformation li a b', 0)->plaintext;} 
$ret[] = $item;  
 } 
}  
foreach($html->find('div.resultsset table tbody.result tr')作为$article){
如果($article->find('span.price',0)){
//零售
$item['Retail']=$article->find('span.price',0)->纯文本;
//索德比
如果($article->find('img',0)->getAttribute('alt')“”){
$item['SoldBy']=$article->find('img',0)->getAttribute('alt');}
else{$item['SoldBy']=$article->find('ul.sellerInformation li a b',0)->明文;}
$ret[]=$item;
} 
}  

你为什么不使用亚马逊API?我正试图通过上传ASIN列表来扫描页面,以便在电子表格中获得所有卖家的价格。我可以用API来做这件事吗?我不确定,但我天真的猜测是你可以。我已经研究过了,amazon API只会从详细信息页面中获取主要报价。不是额外的第三方销售信息。好的,那么您将获得+1的搜索:)