2019-11-18 06:27:00
725

Эта информация будет полезна для тех, кому срочно требуется создать парсер. Парсер довольно прост. 

Чтобы парсер заработал необходимо подключить phpQuery и установить на сайт miniShop2.

<?php
$parts = parse_url($_GET['url']);
print_r($parts);
if($parts['host'] == 'адрессайта.ru'){
   if(include('phpQuery.php')){ echo ''; }         else{ echo ''; }     
       $html = file_get_contents($_GET['url'].'&page='.$_GET['page'].'stop='.$_GET['stop']);
        $pq = phpQuery::newDocument($html);
        $links = $pq->find('.item-inner');

        /** Работаем с информацией в общем списке **/
        foreach ($links as $link) {
           
        $pqLink = pq($link);
        $price = $pqLink->find('.price')->attr('content');
        $nameItem = $pqLink->find('.product-name')->text();
        $propItem = $pqLink->find('.item-prop')->text();
        $imgItem = $pqLink->find('.product-image > img')->attr('src');


            $price = preg_replace('/\s+/', ' ', $price);
            $nameItem  = preg_replace('/\s+/', ' ', $nameItem );
            $propItem = preg_replace('/\s+/', ' ', $propItem);
            $imgItem = preg_replace('/\s+/', ' ', $imgItem);
            
 
        /** Работаем с детальной информацией **/
$detailPrice = file_get_contents($pqLink->find('a')->attr('href'));
$pqDetail = phpQuery::newDocument($detailPrice);
$info = $pqDetail->find('#content_1');
foreach ($info as $infoEl) {
    $pqEl = pq($infoEl);
    $detailItem = $pqEl->find('.htmlDataBlock')->html();
    $detailItem = preg_replace('/\s+/', ' ', $detailItem);
   echo mb_convert_encoding($detailItem, 'utf-8', mb_detect_encoding($detailItem));
}


$arrImg = explode('/', $imgItem);  foreach($arrImg as $endname) { $valname = $endname; }
$pathImage = "downloadImage/copy/".$valname;

echo '<pre>';
if(copy($imgItem,$pathImage)) 
echo 'True Image';
else echo 'False Image';
echo '</pre>';

   $response = $modx->runProcessor('resource/create', array(
        'class_key' => 'msProduct',
        'template' => 3,
        'isfolder' => 0,
        'published' => 1,
        'content' => $detailItem,
        'pagetitle' => $nameItem,
        'parent' => $_GET['parent'],
        'show_in_tree' => 1,
        
        'price' => $price,
        'old_price' => 0,
        'favorite' => 0,
        'popular' => 0,

    ));
    
 if($response->isError()) { return $modx->error->failure($response->getMessage()); }
    $modx->cacheManager->clearCache();
    $id = $response->response['object']['id'];

$gallery = array(
  'id' => $id,
  'name' => '',
  'rank' => 0,
  'file' => MODX_BASE_PATH.$pathImage);
 $upload = $modx->runProcessor('gallery/upload', $gallery, array(
  'processors_path' => MODX_CORE_PATH.'components/minishop2/processors/mgr/'
));

if ($upload->isError()) { print_r($upload->getResponse()); }
}
}

Комментарии (46)