简单 DOM 解析器 IF 不起作用

发布于 2024-12-11 15:43:48 字数 1560 浏览 0 评论 0原文

谁能告诉我为什么第二个 if 语句拉出 sellby 名称不能正常工作吗?

我想要它做的是名称从图像中提取 alt 属性,如果有卖家的图像 - 这有效。但是,如果没有图像徽标,则会有一个带有卖家名称的粗体标签,我希望将其插入到数组中与 alt 标签相同的位置。

我正在使用的 HTML 是:http://amazon.com/gp/offer-listing/B002UYSHMM

有人可以帮忙吗?

谢谢你!

$item = array();

foreach ($html->find('div.resultsset table tbody.result tr') as $article) { 
  if ($article->find('span.price', 0)) {  
    // get retail  
    $item[$retail.$i++] = $article->find('span.price', 0)->plaintext;  
    // get soldby
    if ($article->find('ul.sellerInformation img', 0)) {
      $item[$soldby.$j++] = $article->find('ul.sellerInformation img', 0)->getAttribute('alt');
    } else {
      $item[$soldby.$j++] = $article->find('ul.sellerInformation li a b', 0)->plaintext;
    }
    $ret['SellerInfo'] = $item;
  }
}

这是我从上面的代码中得到的数组:

 Array ( [0] => Array ( [Retail] => $219.88 [SoldBy] => J&R Music and Computer World ) [1] => Array ( [Retail] => $234.21 [SoldBy] => PORTABLE GUY ) [2] => Array ( [Retail] => $235.73 [SoldBy] => The Price Pros ) [3] => Array ( [Retail] => $234.74 [SoldBy] => GizmosForLife ) [4] => Array ( [Retail] => $230.00 [SoldBy] => ) [5] => Array ( [Retail] => $198.73 [SoldBy] => ) [6] => Array ( [Retail] => $240.72 [SoldBy] => ) [7] => Array ( [Retail] => $248.99 [SoldBy] => onSale ) ) 

您可以看到它缺少 SoldBy for:

STEPHS GREAT BOOK TREASURES

Diakonos23

OptimumHouse

Can anyone tell why the second if statement pulling the soldby name wouldn't work properly?

What I want it to do is name pull the alt attribute from the image, if there is an image for the seller - which works. But if there isn't an image logo, there is a bold tag with the seller name which I want to be inserted in the same place in the array that the alt tag would be.

The HTML I am working with is: http:// amazon.com/gp/offer-listing/B002UYSHMM

Can anyone help?

Thank you!

$item = array();

foreach ($html->find('div.resultsset table tbody.result tr') as $article) { 
  if ($article->find('span.price', 0)) {  
    // get retail  
    $item[$retail.$i++] = $article->find('span.price', 0)->plaintext;  
    // get soldby
    if ($article->find('ul.sellerInformation img', 0)) {
      $item[$soldby.$j++] = $article->find('ul.sellerInformation img', 0)->getAttribute('alt');
    } else {
      $item[$soldby.$j++] = $article->find('ul.sellerInformation li a b', 0)->plaintext;
    }
    $ret['SellerInfo'] = $item;
  }
}

Here is the array I get out of my code above:

 Array ( [0] => Array ( [Retail] => $219.88 [SoldBy] => J&R Music and Computer World ) [1] => Array ( [Retail] => $234.21 [SoldBy] => PORTABLE GUY ) [2] => Array ( [Retail] => $235.73 [SoldBy] => The Price Pros ) [3] => Array ( [Retail] => $234.74 [SoldBy] => GizmosForLife ) [4] => Array ( [Retail] => $230.00 [SoldBy] => ) [5] => Array ( [Retail] => $198.73 [SoldBy] => ) [6] => Array ( [Retail] => $240.72 [SoldBy] => ) [7] => Array ( [Retail] => $248.99 [SoldBy] => onSale ) ) 

You can see that it is missing SoldBy for:

STEPHS GREAT BOOK TREASURES

Diakonos23

OptimumHouse

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小猫一只 2024-12-18 15:43:48

除了不使用 b 是明智之举之外,您的选择器也是不同的。第一个直接选择列表中的图像,而第二个需要在列表项内的链接内使用粗体标记。

Apart from it being wise not to use the b, your selectors are different. The first directly selects an image in the list, while the second requires a bold tag inside a link, inside a list item.

一向肩并 2024-12-18 15:43:48

您尝试过使用

if (count($article->find('span.price'))) {

and

if (count($article->find('ul.sellerInformation img'))) {

吗?

Have you tried using

if (count($article->find('span.price'))) {

and

if (count($article->find('ul.sellerInformation img'))) {

?

煮茶煮酒煮时光 2024-12-18 15:43:48

想通了:

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; 
 }
} 

Figured it out:

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; 
 }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文