试图在 C:\xampp\htdocs\tables\index.php 中获取非对象的 属性
Trying to get property of non-object in C:\xampp\htdocs\tables\index.php
我正在使用以下代码,但出现了一些错误提示 -
Trying to get property of non-object in
C:\xampp\htdocs\tables\index.php.
我有以下代码-
<?php
require 'simple_html_dom.php';
$html = file_get_html('http://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.html');
foreach($html->find('div#sellers-table-wrap table tbody tr') as $article) {
$item['name'] = $article->find('td.seller-name div.seller-info span.seller-info-caption', 0)->plaintext;
$item['price'] = $article->find('td.offer-price span.variant-final-price span.m-w', 0)->plaintext;
$articles[] = $item;
}
echo "<pre>";
print_r($articles);
?>
更新:我也试过了,但没用:
foreach($html->find('div#sellers-table-wrap table tbody tr') as $article) {
if (count($article->find('td.seller-name div.seller-info span.seller-info-caption')) > 0) {
$item['name'] = $article->find('td.seller-name div.seller-info span.seller-info-caption', 0)->plaintext;
$item['price'] = $article->find('td.offer-price span.variant-final-price span.m-w', 0)->plaintext;
}
$articles[] = $item;
}
echo "<pre>";
print_r($articles);
将循环中的选择器更改为:
foreach($html->find('div#sellers-table-wrap > table > tbody > tr') as $article) {
此 table 中还嵌套了其他 table,因此您也将遍历它们的行。但是它们没有与您用于分配给 $item['name']
和 $item['price']
的选择器相匹配的元素,因此那些 find()
调用返回 null
,这会导致错误.此选择器仅匹配顶级 table.
中的行
您还可以添加支票:
foreach($html->find('div#sellers-table-wrap > table > tbody > tr') as $article) {
$item = array();
$caption = $article->find('td.seller-name div.seller-info span.seller-info-caption', 0);
$mw = $article->find('td.offer-price span.variant-final-price span.m-w', 0);
if ($caption && $mw) {
$item['name'] = $caption->plaintext;
$item['price'] = $mw->plaintext;
$articles[] = $item;
}
}
我正在使用以下代码,但出现了一些错误提示 -
Trying to get property of non-object in C:\xampp\htdocs\tables\index.php.
我有以下代码-
<?php
require 'simple_html_dom.php';
$html = file_get_html('http://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.html');
foreach($html->find('div#sellers-table-wrap table tbody tr') as $article) {
$item['name'] = $article->find('td.seller-name div.seller-info span.seller-info-caption', 0)->plaintext;
$item['price'] = $article->find('td.offer-price span.variant-final-price span.m-w', 0)->plaintext;
$articles[] = $item;
}
echo "<pre>";
print_r($articles);
?>
更新:我也试过了,但没用:
foreach($html->find('div#sellers-table-wrap table tbody tr') as $article) {
if (count($article->find('td.seller-name div.seller-info span.seller-info-caption')) > 0) {
$item['name'] = $article->find('td.seller-name div.seller-info span.seller-info-caption', 0)->plaintext;
$item['price'] = $article->find('td.offer-price span.variant-final-price span.m-w', 0)->plaintext;
}
$articles[] = $item;
}
echo "<pre>";
print_r($articles);
将循环中的选择器更改为:
foreach($html->find('div#sellers-table-wrap > table > tbody > tr') as $article) {
此 table 中还嵌套了其他 table,因此您也将遍历它们的行。但是它们没有与您用于分配给 $item['name']
和 $item['price']
的选择器相匹配的元素,因此那些 find()
调用返回 null
,这会导致错误.此选择器仅匹配顶级 table.
您还可以添加支票:
foreach($html->find('div#sellers-table-wrap > table > tbody > tr') as $article) {
$item = array();
$caption = $article->find('td.seller-name div.seller-info span.seller-info-caption', 0);
$mw = $article->find('td.offer-price span.variant-final-price span.m-w', 0);
if ($caption && $mw) {
$item['name'] = $caption->plaintext;
$item['price'] = $mw->plaintext;
$articles[] = $item;
}
}