我在解析方面遇到一些问题,我需要获取照片链接,但代码中存在错误。PHP错误遇到了严重性:注意消息:试图获取非对象文件名的属性: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" />
有帮助吗?

解决方案

您收到此警告是因为您的查找查询不返回任何节点对象,因为没有带有 img 作为属性。

如果我理解得很好,您正在尝试查找一个 span 节点的文本内容,该节点的子节点是具有“data-original”属性的 img 节点。所以语法是(因为 img 是一个节点而不是属性):

$photo=$phone->find('span img[data-original]',0)->plaintext;

如果您的目标是获取图像链接(隐藏在 data-original 非标准属性,您必须使用:

$photo=$phone->find('span img[data-original]',0)->attr['data-original'];

其他提示

您将$ photo作为如此如此:

$photo=$phone->find('span[img=data-original]',0)->plaintext;
.

然后你试图像对象一样对待它:

$linkas = $phone->href;
.

无法完成。如果您想要一个对象,请不要使用plaintext

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top