Php 解析信息
我试图解析有关手机的信息。需要电话:链接,名称,价格,照片现在我有:链接,名称,价格。所以我只需要一点点。我的代码现在如下所示:Php 解析信息,php,html,parsing,simple-html-dom,Php,Html,Parsing,Simple Html Dom,我试图解析有关手机的信息。需要电话:链接,名称,价格,照片现在我有:链接,名称,价格。所以我只需要一点点。我的代码现在如下所示: 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 $ne
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+),?@', $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);
}
?>
h2>Telefonai Varle su照片
要检索评级,您必须计算span[class=rating]中span[class=selected]的数量
$rating = count($phone->find('span[class=rating]')->find('span[class=selected]'));
那手机照片呢?这不起作用。致命错误:对中的非对象调用成员函数find()
$rating = count($phone->find('span[class=rating]')->find('span[class=selected]'));