2014-04-13 48 views
0

我尝试解析关于手机的信息。需要电话:链接,名称,价格,照片现在我有:链接,名称,价格。所以需要一点点。我的代码现在看起来像这样:解析信息

h2>Telefonai Varle su photo</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); 

     $phones=$html->find(); 
     ///////////////////////////////////////////////////////////// 
     /// Get phone blocks and extract info (also insert to db) /// 
     ///////////////////////////////////////////////////////////// 
     $phones = $html->find('a[data-id]'); 

    foreach($phones as $phone) 
    { 
     // Get the link 
     $linkas = $phone->href; 
     // Get photo 
     $foto= $phone->find('span[data-original]', 0)->plaintext; 
     // 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; 

     preg_match('@(\d+),[email protected]', $kaina, $matches); 
     $kaina = $matches[1]; 

     echo $pavadinimas, " #----# ", $kaina, " #----# ", $linkas, " nuotrauka " , $foto   ,"<br>"; 

     //$query = "insert into telefonai (pavadinimas,kaina,parduotuve,linkas,foto) VALUES (?,?,?,?,?)"; 
     //$this->db->query($query, array($pavadinimas,$kaina,"Varle.lt", $linkas,"https://www.varle.lt"+$foto)); 
    } 

    ///////////////////////////////////////////////////////////// 
    ///////////////////////////////////////////////////////////// 

    // 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/samsung-phone-i9300-galaxy-s3-  juodasbaltas.html" class="grid-item product " data-id="426226" 
title=" Samsung Phone I9300 Galaxy S3 Juodas/Baltas - 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 witem426226" data-id="426226"> 
     <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/26/sam/samsung-phone-i9300-galaxy-siii-onyx-black.jpg" 
     alt=" Samsung Phone I9300 Galaxy S3 Juodas/Baltas - Mobilieji telefonai" /> 


    </span> 
<span class="title"><span class="inner"><span> Samsung Phone I9300 Galaxy S3 Juodas/Baltas</span></span></span> 


<span class="prices"> 

<span class="old-price-discount"> 
    <span class="old-price"> 
     1149 
     Lt<span></span> 
    </span><br /> 
    <span class="discount"><span class="minus">-</span>290 Lt</span> 
</span> 

<span class="price"> 
    859 Lt 
</span> 

</span> 
<span class="rating"> 

<span class="rating-star selected"></span> 

<span class="rating-star selected"></span> 

<span class="rating-star selected"></span> 

<span class="rating-star selected"></span> 

<span class="rating-star "></span> 

<span class="count">(85)</span> 
</span> 

有人可以帮我解决这个问题吗?

回答

0

要检索的评价,你要算跨度的跨度里面有什么关于手机照片数量[CLASS =选择] [CLASS =等级]

$rating = count($phone->find('span[class=rating]')->find('span[class=selected]')); 
+0

和? – Mangirdas

+0

,这不起作用。致命错误:在一个非对象中调用成员函数find() – Mangirdas