Question

quels sont les avantages et les inconvénients des bibliothèques suivantes?

De ce qui précède, je l'ai utilisé QP et Raté pour analyser HTML invalide, et simpleDomParser, qui fait un bon travail, mais il fuit un peu la mémoire à cause du modèle d'objet. Mais vous pouvez garder que sous contrôle en appelant $object->clear(); unset($object); lorsque vous ne avez pas besoin d'un objet plus.

Y at-il plus grattoirs? Quelles sont vos expériences avec eux? Je vais faire un wiki communautaire, nous pouvons construire une liste utile des bibliothèques qui peuvent être utiles lors de grattage.


je l'ai fait des tests sur la base de la réponse de Byron:

    <?
    include("lib/simplehtmldom/simple_html_dom.php");
    include("lib/phpQuery/phpQuery/phpQuery.php");


    echo "<pre>";

    $html = file_get_contents("http://stackoverflow.com/search?q=favorite+programmer+cartoon");
    $data['pq'] = $data['dom'] = $data['simple_dom'] = array();

    $timer_start = microtime(true);

    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $x = new DOMXPath($dom);

    foreach($x->query("//a") as $node)
    {
         $data['dom'][] = $node->getAttribute("href");
    }

    foreach($x->query("//img") as $node)
    {
         $data['dom'][] = $node->getAttribute("src");
    }

    foreach($x->query("//input") as $node)
    {
         $data['dom'][] = $node->getAttribute("name");
    }

    $dom_time =  microtime(true) - $timer_start;
    echo "dom: \t\t $dom_time . Got ".count($data['dom'])." items \n";






    $timer_start = microtime(true);
    $doc = phpQuery::newDocument($html);
    foreach( $doc->find("a") as $node)
    {
       $data['pq'][] = $node->href;
    }

    foreach( $doc->find("img") as $node)
    {
       $data['pq'][] = $node->src;
    }

    foreach( $doc->find("input") as $node)
    {
       $data['pq'][] = $node->name;
    }
    $time =  microtime(true) - $timer_start;
    echo "PQ: \t\t $time . Got ".count($data['pq'])." items \n";









    $timer_start = microtime(true);
    $simple_dom = new simple_html_dom();
    $simple_dom->load($html);
    foreach( $simple_dom->find("a") as $node)
    {
       $data['simple_dom'][] = $node->href;
    }

    foreach( $simple_dom->find("img") as $node)
    {
       $data['simple_dom'][] = $node->src;
    }

    foreach( $simple_dom->find("input") as $node)
    {
       $data['simple_dom'][] = $node->name;
    }
    $simple_dom_time =  microtime(true) - $timer_start;
    echo "simple_dom: \t $simple_dom_time . Got ".count($data['simple_dom'])." items \n";


    echo "</pre>";

et obtenu

dom:         0.00359296798706 . Got 115 items 
PQ:          0.010568857193 . Got 115 items 
simple_dom:  0.0770139694214 . Got 115 items 
Était-ce utile?

La solution

Je l'habitude d'utiliser dom simple html exclusivement jusqu'à ce que quelques SO'ers lumineux m'a montré la lumière alléluia.

Il suffit d'utiliser les fonctions intégrées DOM. Ils sont écrits en C et une partie du noyau de PHP. Ils sont plus rapides plus efficaces que toute solution 3ème partie. Avec Firebug, obtenir une requête XPath est Muey simple. Ce simple changement a mes grattoirs à base php courir plus vite, tout en économisant mon temps précieux.

Mes racleurs utilisés pour prendre ~ 60 méga-octets pour gratter asyncronously 10 sites avec boucle. Ce fut même avec la solution simple mémoire dom html vous avez mentionné.

Maintenant, mes processus php ne vont jamais au-dessus de 8 méga-octets.

Hautement recommandé.

modifier

D'accord, je l'ai fait quelques points de repère. Construit en dom est au moins un ordre de grandeur plus rapide.

Built in php DOM: 0.007061
Simple html  DOM: 0.117781

<?
include("../lib/simple_html_dom.php");

$html = file_get_contents("http://stackoverflow.com/search?q=favorite+programmer+cartoon");
$data['dom'] = $data['simple_dom'] = array();

$timer_start = microtime(true);

$dom = new DOMDocument();
@$dom->loadHTML($html);
$x = new DOMXPath($dom); 

foreach($x->query("//a") as $node) 
{
     $data['dom'][] = $node->getAttribute("href");
}

foreach($x->query("//img") as $node) 
{
     $data['dom'][] = $node->getAttribute("src");
}

foreach($x->query("//input") as $node) 
{
     $data['dom'][] = $node->getAttribute("name");
}

$dom_time =  microtime(true) - $timer_start;

echo "built in php DOM : $dom_time\n";

$timer_start = microtime(true);
$simple_dom = new simple_html_dom();
$simple_dom->load($html);
foreach( $simple_dom->find("a") as $node)
{
   $data['simple_dom'][] = $node->href;
}

foreach( $simple_dom->find("img") as $node)
{
   $data['simple_dom'][] = $node->src;
}

foreach( $simple_dom->find("input") as $node)
{
   $data['simple_dom'][] = $node->name;
}
$simple_dom_time =  microtime(true) - $timer_start;

echo "simple html  DOM : $simple_dom_time\n";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top