Witam, od jakiegoś czasu męcze się z problemem którym jest właściwe pobbranie danych z plików HTML-a które zawsze mają identyczną strukturę. Poniżej moja struktura bazy danych :
-
-- Struktura tabeli dla `crawler_produkty`
--
CREATE TABLE IF NOT EXISTS `crawler_produkty` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`aktywnosc` tinyint(1) NOT NULL DEFAULT '1',
`nazwa` varchar(250) NOT NULL,
`cena` decimal(10,2) NOT NULL,
`podatek_VAT` varchar(3) NOT NULL DEFAULT '23%',
`jednostka` varchar(4) NOT NULL DEFAULT 'szt.',
`kategoria` int(11) NOT NULL,
`krotki_opis` text NOT NULL,
`opis` text NOT NULL,
`magazyn` int(11) NOT NULL,
`dostepnosc` varchar(255) NOT NULL,
`zdjecia` text NOT NULL,
`zastosowanie` text NOT NULL,
` zamienniki` int(11) NOT NULL,
`nr_prducenta` int(11) NOT NULL,
`producent` varchar(255) NOT NULL,
`nr_oryginalu` int(11) NOT NULL,
`numer_polcar` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=125 ;
Tutaj znajduje sie kod skryptu w PHP wyciągającego dane z plików, Nie wiem dlaczego ale skrypt ten nie zbiera wszystkich wpisów z pliku przez co z bazy Nie wyciąga wszystkich ID aut. Proszę was o pomoc!
<?php
set_time_limit ( 0 ); //wyzerowanie limitu czasu wykonywania dla skryptu
require_once('db.php'); //połączenie z bazą danych
/*
*
*/
$dir = 'sciezka do katalogu w którym są pliki';
function scan($url, $id){
$original_file = file_get_contents($url);
if(strpos($original_file, 'skp-karta-prod') !== false){
getSkp($original_file, $id);
usleep(1);
}
}
function getSkp($html, $url){
$start = strpos($html, 'skp-karta-prod');
$substr = substr($html, $start);
$start = strpos($substr, '</form>');
$skp = mysql_real_escape_string(str_replace('skp-karta-prod fix">' , '', substr($substr, 0, $start)));
$start = strpos($html, '<h3>');
$substr = substr($html, $start);
$start = strpos($substr, '</h3>');
$_name = mysql_real_escape_string(str_replace('<h3>' , '', substr($substr, 0, $start)));
$start = strpos($html, '<h4>');
$substr = substr($html, $start);
$start = strpos($substr, '</h4>');
$_owner = mysql_real_escape_string(str_replace(' ' ,'', str_replace('<h4>' , '', substr($substr, 0, $start))));
$start = strpos($html, '<p class="">Kod produktu: <span class="">');
$substr = substr($html, $start);
$start = strpos($substr, '</span></p>');
$_code = mysql_real_escape_string(str_replace('<p class="">Kod produktu: <span class="">' , '', substr($substr, 0, $start)));
$start = strpos($html, '<p class="skp-cena">Cena: <b class="skp-cena-karta-produktu">');
$substr = substr($html, $start);
$start = strpos($substr, '</b>');
$_price = str_replace(',','.',str_replace('<p class="skp-cena">Cena: <b class="skp-cena-karta-produktu">' , '', substr($substr, 0, $start)));
$_price =(float)$_price;
$start = strpos($html, '<h5>Informacje:</h5>');
if ($start !== false) {
$substr = substr($html, $start);
$start = strpos($substr, '</ul>');
$_short_desc = str_replace('<ul class="karta-produktu-atrybuty">' , '', substr($substr, 0, $start));
$_short_desc = str_replace('<h5>Informacje:</h5>' , '', $_short_desc);
$_short_desc = nl2br(trim(strip_tags($_short_desc)));
$_short_desc=mysql_real_escape_string($_short_desc);
}else
$_short_desc='';
$start = strpos($html, '<h5>Dodatkowe informacje:</h5>');
if ($start !== false) {
$substr = substr($html, $start);
$start = strpos($substr, '</ul>');
$_long_desc = str_replace('<ul class="karta-produktu-atrybuty">' , '', substr($substr, 0, $start));
$_long_desc = str_replace('<h5>Dodatkowe informacje:</h5>' , '', $_long_desc);
$_long_desc = nl2br(trim(strip_tags($_long_desc)));
$_long_desc=mysql_real_escape_string($_long_desc);
}else
$_long_desc='';
if($_short_desc!='')
$_short_desc.='<br/>'.$_long_desc;
else
$_short_desc.=$_long_desc;
$_long_desc='';
$start = strpos($html, '<p class="skp-dostepnosc">Dostępność towaru: <b>');
$substr = substr($html, $start);
$start = strpos($substr, '</b>');
$_magazyn_name = mysql_real_escape_string(str_replace('<p class="skp-dostepnosc">Dostępność towaru: <b>' , '', substr($substr, 0, $start)));
$auto = str_replace('-od-','',$url);
$oddaj=array();
$pobierz_url=preg_match_all('#\<a href=\"/auto/(.*?)\>(.*?)\</a\>(.*?)\</td\>(.*?)\<td\>(.*?)\</td\>(.*?)\<td\>(.*?)\</td\>(.*?)\<td\>(.*?)\</td\>(.*?)\<td\>(.*?)\</td\>#is',$html,$oddaj );
$auta=$oddaj[2];
//print_r($oddaj[2]);
//print_r($oddaj[11]);
$auta_id='';
if(!empty($auta))
{
$i=0;
$temp=array();
foreach($auta as $a)
{
$tempA=explode('-',$oddaj[11][$i]);
if($tempA[1]==' .')
$a=$a.$tempA[0].'-';
else
$a=$a.$oddaj[11][$i];
$result = mysql_fetch_row(mysql_query("SELECT auto_id FROM auto WHERE REPLACE(LOWER(CONCAT( marka, ' ', model, ' ', pojemnosc, ' ', opis )),' ','')= REPLACE(LOWER('$a'),' ','') LIMIT 1"));
usleep(0.2);
if($result[0] > 0)
{
if(!in_array($result[0],$temp))
{
if($auta_id=='')
{
$auta_id.=$result[0];
$temp[]=$result[0];
}
else
{$auta_id.=', '.$result[0];
$temp[]=$result[0];}
}
}
//echo( $a.'-'.$result[0].'\n');
$i++;
}
}
mysql_query("INSERT INTO `crawler_produkty` (`nazwa`,`cena`,`krotki_opis`,`opis`,`magazyn`,`dostepnosc`,`zastosowanie`,`producent`,`numer_polcar`) VALUES
('$_name','$_price','$_short_desc','$_long_desc','5','$_magazyn_name','$auta_id','$_owner','$_code');");
}
open_dir($dir);
function open_dir($dir) {
global $files_counter;
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file != '..' && $file != '.') {
if(is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
open_dir($dir . DIRECTORY_SEPARATOR . $file);
} else {
$tmp = explode(',', $file);
$id = substr(implode(',',array_slice($tmp,1)), 0, -5);
//echo "$id";
scan($dir . DIRECTORY_SEPARATOR . $file, $id);
//echo "<br/>";
$files_counter++;
}
}
}
closedir($dh);
}
}
echo $files_counter;
?>
Tutaj znajdują się 3 przykładowe pliki HTML-a z których wyciągam dane oraz strukura mojej bazy z autami (potrzebna do przypisania ID auta w polu zastosowanie)