非オブジェクトの財産を取得しようとしている(解析)
-
21-12-2019 - |
質問
私は解析についていくつかの問題を抱えています、私は写真リンクを取得する必要がありますが、エラーはコードです。 PHPエラーが発生しました 重大度:通知 メッセージ:非オブジェクトの財産を取得しようとしている filename:ビュー/ varle2_view.php. 行番号:25
<h2>Telefonai Varle</h2>
</br>
<?php
include_once('simple_html_dom.php');
$url = "https://www.varle.lt/mobilieji-telefonai/";
// Start from the main page
$nextLink = $url;
// Loop on each next Link as long as it exsists
while ($nextLink) {
echo "<hr>nextLink: $nextLink<br>";
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a url
$html->load_file($nextLink);
/////////////////////////////////////////////////////////////
/// Get phone blocks and extract info (also insert to db) ///
/////////////////////////////////////////////////////////////
$phones = $html->find('a[data-id]');
foreach($phones as $phone) {
$photo=$phone->find('span[img=data-original]',0)->plaintext;
// Get the link
$linkas = $phone->href;
// Get the name
$pavadinimas = $phone->find('span[class=inner]', 0)->plaintext;
$pavadinimas = str_replace("Išmanusis telefonas"," ",$pavadinimas);
// Get the name price and extract the useful part using regex
$kaina = $phone->find('span[class=price]', 0)->plaintext;
// This captures the integer part of decimal numbers: In "123,45" will capture "123"... Use @([\d,]+),?@ to capture the decimal part too
preg_match('@(\d+),?@', $kaina, $matches);
$kaina = $matches[1];
echo $pavadinimas, " #----# ", $kaina, " #----# ", $linkas, "#----#", $photo, " <br>";
//$query = "insert into telefonai (pavadinimas,kaina,parduotuve,linkas) VALUES (?,?,?,?)";
// $this->db->query($query, array($pavadinimas,$kaina,"Varle.lt", $linkas));
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// Extract the next link, if not found return NULL
$nextLink = ( ($temp = $html->find('div.pagination a[class="next"]', 0)) ? "https://www.varle.lt".$temp->href : NULL );
// Clear DOM object
$html->clear();
unset($html);
}
?>
.
Webのソース
<a href="https://www.varle.lt/mobilieji-telefonai/htc-desire-500-dual-sim-glossy-juodas-506e.html" class="grid-item product " data-id="606846"
title=" HTC DESIRE 500 Dual SIM GLOSSY JUODAS 506e - Mobilieji telefonai">
<span class="left-border"></span>
<span class="left-border-hover"></span>
<span class="right-border-hover"></span>
<span class="top-border-hover"></span>
<span class="bottom-border-hover"></span>
<span class="wishlist_button_cont">
<span class="add_to_wishlist witem606846" data-id="606846">
<span class="icon insert"></span>
</span>
</span>
<span class="img-container" style="position: relative;">
<img src="/static/app/img/white_space.png?lazyyy" class="lazy" data-original="/static/uploads/products/235x195/2/htc/htc-desire-500-dual-sim-glossy-black-506e_3.jpg"
alt=" HTC DESIRE 500 Dual SIM GLOSSY JUODAS 506e - Mobilieji telefonai" />
. 解決
属性としてimg
を持つスパンタグがないため、検索クエリはノードオブジェクトを返さないため、この警告を入手します。
私がうまく理解している場合は、 `data-original '属性を持つIMGノードとしてあるSPANノードのテキスト内容を見つけようとしています。そのため、構文は(IMGが属性ではなくノードであるため)です。
$photo=$phone->find('span img[data-original]',0)->plaintext;
.
あなたの目標が画像リンクを取得することの場合(それはdata-original
以外の標準属性の内側に隠されていますが、使用する必要があります。
$photo=$phone->find('span img[data-original]',0)->attr['data-original'];
. 他のヒント
このような文字列として$写真を入手しています。
$photo=$phone->find('span[img=data-original]',0)->plaintext;
.
そしてあなたはそれをオブジェクトのように扱おうとしています:
$linkas = $phone->href;
.
はできません。オブジェクトが必要な場合はplaintext
を使用しないでください。
所属していません StackOverflow