Skip to content

Instantly share code, notes, and snippets.

@andreybolonin
Created December 23, 2016 19:52
Show Gist options
  • Save andreybolonin/c79b035ceb366df71bce5cc21f853033 to your computer and use it in GitHub Desktop.
Save andreybolonin/c79b035ceb366df71bce5cc21f853033 to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DomCrawler\Crawler;
use Goutte\Client;
use AppBundle\Entity\Notebook;
class NotebookCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('parse:notebooks')
->setDescription('Parses notebooks from Hotline.ua')
->setHelp("")
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = new Client();
$crawler = $client->request('GET', 'http://hotline.ua/computer/noutbuki-netbuki/385943-883-85763-85764-85765/');
$em = $this->getContainer()->get('doctrine')->getManager();
$crawler_result = $crawler->filter('img.max-120');
$crawler2 = $crawler->filter('div.gd-price-cell')->filter('div.text-14.text-13-480.orng');
$count = 0;
foreach ($crawler_result as $key => $el) {
$notebook = new Notebook();
$notebook->setImage('http://hotline.ua' . $el->getAttribute('src'));
$notebook->setTitle($el->getAttribute('alt'));
$notebook->setPrice($crawler2->getNode($key)->textContent);
$em->persist($notebook);
$count++;
}
$em->flush();
$output->writeln([
'Count of added records:'.$count,
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment