简单 DOM 解析器 IF 不起作用
谁能告诉我为什么第二个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了不使用
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.您尝试过使用
and
吗?
Have you tried using
and
?
想通了:
Figured it out: