비 객체의 속성을 얻으려고 (구문 분석)
-
21-12-2019 - |
문제
구문 분석에 대해 몇 가지 문제가 있지만 사진 링크를 얻을 필요가 있지만 오류가 코드에 있습니다. PHP 오류가 발생했습니다 심각도 : 알림 메시지 : 비 객체의 속성을 얻으려고합니다. filename : views / 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);
}
?>
.
웹 소스
<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" />
. 해결책
PRACE 쿼리가 img
가있는 SPAN 태그가없는 SPAN 태그가 없으므로이 경고를 얻으십시오.
잘 이해하면`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;
.
할 수 없습니다.Object를 원하시면 plaintext
를 사용하지 마십시오.
제휴하지 않습니다 StackOverflow