我想在我的数据库中插入一些元素,但我希望$ pavadinimas和%kaina在一行中,而不是不同。此外,如果我可以在网站的所有页面中生成我的元素,那将是非常酷的,但随后我插入了2个以上的链接我从刷新我的网页无法加载错误。这是我的代码。感谢帮助!
<?php // example of how to modify HTML contents
include_once('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('https://www.varle.lt/mobilieji-telefonai/');
foreach($html->find('span[class=inner]') as $pavadinimas) {
$pavadinimas = str_replace("<span class=", " ", $pavadinimas);
$pavadinimas = str_replace("inner>", " ", $pavadinimas);
$pavadinimas = str_replace("<span>", " ", $pavadinimas);
$pavadinimas = str_replace("</span></span>", " ", $pavadinimas);
$pavadinimas = str_replace('"inner"> ', " ", $pavadinimas);
}
foreach($html->find('span[class=price]') as $kaina) {
$kaina = str_replace("Lt", " ", $kaina);
$kaina = str_replace("<span class=", " ", $kaina);
$kaina = str_replace("price", " ", $kaina);
$kaina = str_replace("</span>", " ", $kaina);
$kaina = str_replace(",<sup>99</sup>", " ", $kaina);
$kaina = str_replace(",<sup>99</sup>", " ", $kaina);
$kaina = str_replace(" ", " ", $kaina);
$kaina = str_replace('" ">', " ", $kaina);
$kaina = str_replace(" ", " ", $kaina);
$query = "insert into telefonai (pavadinimas,kaina) VALUES (?,?)";
$this->db->query($query, array($pavadinimas,$kaina));
}
?>
答案 0 :(得分:0)
一步一步地进行......
首先从一个页面获取所有想要的信息(例如第一个)......想法是:
$phones = $html->find('a[data-id]');
现在你已经让代码适用于一个页面了,让我们试着让它适用于所有知道:
的页面Next
按钮中,因此我们会在无法找到此链接时停止所以这里是一个代码,总结了我们上面所说的所有内容:
$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) {
// Get the link
$linkas = $phone->href;
// Get the name
$pavadinimas = $phone->find('span[class=inner]', 0)->plaintext;
// 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, "<br>";
// INSERT INTO DB HERE
// CODE
// ...
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// 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);
}
<强>输出强>
nextLink: https://www.varle.lt/mobilieji-telefonai/
Samsung Phone I9300 Galaxy SIII Juodas #----# 1099 #----# https://www.varle.lt/mobilieji-telefonai/samsung-phone-i9300-galaxy-siii-juodas.html
Samsung Galaxy S2 Plus I9105 Pilkai mėlynas #----# 739 #----# https://www.varle.lt/mobilieji-telefonai/samsung-galaxy-s2-plus-i9105-pilkai-melynas.html
Samsung Phone S7562 Galaxy S Duos baltas #----# 555 #----# https://www.varle.lt/mobilieji-telefonai/samsung-phone-s7562-galaxy-s-duos-baltas--457135.html
...
nextLink: https://www.varle.lt/mobilieji-telefonai/?p=2
LG T375 Mobile Phone Black #----# 218 #----# https://www.varle.lt/mobilieji-telefonai/lg-t375-mobile-phone-black.html
Samsung S6802 Galaxy Ace Duos black #----# 579 #----# https://www.varle.lt/mobilieji-telefonai/samsung-s6802-galaxy-ace-duos-black.html
Mobilus telefonas Samsung Galaxy Ace Onyx Black | S5830 #----# 559 #----# https://www.varle.lt/mobilieji-telefonai/mobilus-telefonas-samsung-galaxy-ace-onyx-black.html
...
...
...
注意代码可能需要一段时间来解析所有页面,因此php可能会返回此错误Fatal error: Maximum execution time of 30 seconds exceeded ...
。然后,只需延长最长执行时间,如下所示:
ini_set('max_execution_time', 300); //300 seconds = 5 minutes